This question already has answers here:
Run JavaScript code on window close or page refresh?
(8 answers)
Closed 1 year ago.
I have a site I'm building where a user clicks a row from a datagrid and it opens a new Chrome window with a form filled with data from the row. I only want one user editing the data at a time so if another user gets in, the screen is read-only. I do this by setting a LockUserID and LockDate on the row after a user enters. The minute the first user (lockuser) saves the data it will close the window and null out the LockUserID and LockDate so then next person can get in.
My problem is if a user clicks the chrome x button I don't know that the user left therefore the LockUserID and LockDate still have a value on the row and anyone that gets in afterward will have a read only view.
I have tried using before unload but this almost feels like a hack considering beforeunload is used for refreshing and other events.
Does anyone have any better suggestions with locking?
Maybe you can send a regular AJAX request to the server. Let's say every 30 seconds. The server saves the time. When a new user tries to open the side you can check if the interval is bigger between the last incoming request and the current time than the 40 seconds and if so unlock the site.
Not the best solution i think but it should work
Or the better solution: Run JavaScript code on window close or page refresh?
Related
First, I will start out saying I understand the concept that a window not opened by you with JavaScript cannot be closed by you using window.close(). What I am wondering is if in Rails (Rails-7 for me), is there a way, perhaps using an initialize script, to open the default window using window.open(). What I am trying to accomplish is, a user scans a QR code that directs to another path in my routes, it will display a message and screen colour change for 10 seconds. At this point, I want that tab plus the initial opening default tab to close, and the application is then closed. The second tab is no problem but I need to be able to close the beginning tab as well, otherwise, the user will end up with a new tab for every QR they scan, which could result in hundreds in the course of a day. Thanks in advance for any input.
I have to set the flag (isOpen = 0) when user is leaving the site while clicking the 'Leave' button. Is there a direct way to catch the Leave button click event?
Edited:
We are developing a process flow diagram application. One diagram can access by multiple users in the same group, in that case, if one user open the diagram for edit purpose then system should not allow to edit for another user. So we have set flag isOpen =1 when open the diagram, once closed (customized close button from the page) we set isOpen=0. These scenarios are working fine except user is closing the browser window.
Browser closing scenario:
Please refer the top image. If user close the tab then browser will ask to confirm the action., in that case user
1) User shall leave
2) User shall cancel and stay the page
I cannot set isOpen=0 for window.onbeforeunload() event because if user shall cancel and stay the same page. So we need to catch the Leave/Cancel user actions to set the flag.
Please suggest me an alternate solution if it is not possible to catch leave/cancel user actions.
What you want to achieve is not possible using this logic, the reason is that when a browser tab is closed, all JS execution stops (quite logically since the tab was closed).
Al alternative way to do this is to solve the problem differently. Every couple of seconds (or minutes, depending on your use case) you can send a fetch/ajax request as an "I'm connected" flag. When a user stops sending such a flag it means they closed the tab or it became inactive. If the user reopens it, you will start receiving that user's flag again.
This can be solved more elegantly using WebSockets, which does exactly what I outlined above out of the box. You just need to keep track of your users by their "flag", be it an ID, cookie, etc.
I have a Live Feed jQuery box which updates in every 10 seconds, and puts the sites latest comments on top. This is working fine: jQuery makes an Ajax request, calls a PHP, which returns new items or none.
This box is like a sidebar, it is on every page on my site. My problem is that if a user opens many pages on the site, every tab he opened will do this auto-refresh until he closes that tab. So with a few dozen users each opening many pages this becomes a problem, even if the Live Feed is well optimized, and the SQL query behind it is fast (0.0005 seconds per query). Also if the user leaves the browser open with a couple of opened tabs, and start browsing somewhere else, or watch a movie they'll update forever, or until he closes them.
So what is a nice solution for this? Can I make my feed update only if its tab/window is visible/active? Is there an event which will fire if it was inactive and now active again?
Try adding onFocus event on the window object to trigger your updates and add onBlur to stop updating your live feeds.
Since you are using jquery you can do this
$(window).('focus',function(){
//do updates
})
$(window).('blur',function(){
//stop updates
})
I am creating an internal web based application that will not be the target audience of the web.
I understand the frustration of alert boxes and forcing people to do certain things.
With that said, what I am attempting to do is create a javascript function, that unless a user clicks a link on a specific page, if they try to navigate away from the page other than using a link on the page, I would like to alert them and say, sorry you need to click the appropriate link to exit.
What my issue is, is that I need to lock out fields, and what I can do when a user hits an edit page, im going to write to a table that user to the lockoutuser colum. If a value exist, that user can access the record if it is null, it means no one is editing the record. If someone clicks to go into that record they lock it out, my means of updating the lockoutuser colum could be ajaxy on unload of the page, but the page could be unloaded for 2 reasons, 1 the edit form is submitted or the user leaves the page.
An alert that would say, sorry you can leave this record without clicking the big red button that says unlock, and force the user without refreshing to stay on the page.
I understand the machine could crash and or an alt f4 or a brute end task on the browser will still leave me other work to unlock the record
You need to use the onunload event of the page to present a messagebox when the user tries to leave your page. Check out this example: http://www.codetoad.com/javascript/miscellaneous/onunload_event_eg.asp
Once the user is on my page, I do not want him to refresh the page.
Anytime, the user hits F5 or refresh button on top. He should get an alert saying
You cannot refresh the page.
Also if the user opens a new tab and tries to access the same url in prev tab he should get an alert
You cannot open same page in 2 tabs
Anyway I can do this using JavaScript or jQuery? Point one is really important.
#1 can be implemented via window.onbeforeunload.
For example:
<script type="text/javascript">
window.onbeforeunload = function() {
return "Dude, are you sure you want to leave? Think of the kittens!";
}
</script>
The user will be prompted with the message, and given an option to stay on the page or continue on their way. This is becoming more common. Stack Overflow does this if you try to navigate away from a page while you are typing a post. You can't completely stop the user from reloading, but you can make it sound real scary if they do.
#2 is more or less impossible. Even if you tracked sessions and user logins, you still wouldn't be able to guarantee that you were detecting a second tab correctly. For example, maybe I have one window open, then close it. Now I open a new window. You would likely detect that as a second tab, even though I already closed the first one. Now your user can't access the first window because they closed it, and they can't access the second window because you're denying them.
In fact, my bank's online system tries real hard to do #2, and the situation described above happens all the time. I usually have to wait until the server-side session expires before I can use the banking system again.
You can't prevent the user from refreshing, nor should you really be trying. You should go back to why you need this solution, what's the root problem here?. Start there and find a different way to go about solving the problem. Perhaps is you elaborated on why you think you need to do this it would help in finding such a solution.
Breaking fundamental browser features is never a good idea, over 99.999999999% of the internet works and refreshes with F5, this is an expectation of the user, one you shouldn't break.
Although its not a good idea to disable F5 key you can do it in JQuery as below.
<script type="text/javascript">
function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
$(document).ready(function(){
$(document).on("keydown", disableF5);
});
</script>
Hope this will help!
Back in the ole days of CGI we had many forms that would trigger various backend actions. Such as text notifications to groups, print jobs, farming of data, etc.
If the user was on a page that was saying "Please wait... Performing some HUGE job that could take some time.". They were more likely to hit REFRESH and this would be BAD!
WHY? Because it would trigger more slow jobs and eventually bog down the whole thing.
The solution?
Allow them to do their form.
When they submit their form... Start your job and then direct them to another page that tells them to wait.
Where the page in the middle actually held the form data that was needed to start the job.
The WAIT page however contains a javascript history destroy. So they can RELOAD that wait page all they want and it will never trigger the original job to start in the background as that WAIT page only contains the form data needed for the WAIT itself.
Hope that makes sense.
The history destroy function also prevented them from clicking BACK and then refreshing as well.
It was very seamless and worked great for MANY MANY years until the non-profit was wound down.
Example:
FORM ENTRY - Collect all their info and when submitted, this triggers your backend job.
RESPONSE from form entry - Returns HTML that performs a redirect to your static wait page and/or POST/GET to another form (the WAIT page).
WAIT PAGE - Only contains FORM data related to wait page as well as javascript to destroy the most recent history. Like (-1 OR -2) to only destroy the most recent pages, but still allows them to go back to their original FORM entry page.
Once they are at your WAIT page, they can click REFRESH as much as they want and it will never spawn the original FORM job on the backend. Instead, your WAIT page should embrace a META timed refresh itself so it can always check on the status of their job. When their job is completed, they are redirected away from the wait page to whereever you wish.
If they do manually REFRESH... They are simply adding one more check of their job status in there.
Hope that helps. Good luck.
No, there isn't.
I'm pretty sure there is no way to intercept a click on the refresh button from JS, and even if there was, JS can be turned off.
You should probably step back from your X (preventing refreshing) and find a different solution to Y (whatever that might be).
Issue #2 now can be solved using BroadcastAPI.
At the moment it's only available in Chrome, Firefox, and Opera.
var bc = new BroadcastChannel('test_channel');
bc.onmessage = function (ev) {
if(ev.data && ev.data.url===window.location.href){
alert('You cannot open the same page in 2 tabs');
}
}
bc.postMessage(window.location.href);
Number (2) is possible by using a socket implementation (like websocket, socket.io, etc.) with a custom heartbeat for each session the user is engaged in. If a user attempts to open another window, you have a javascript handler check with the server if it's ok, and then respond with an error messages.
However, a better solution is to synchronize the two sessions if possible like in google docs.