This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How we call logout servlet on browser close event
I want my site to automatically logout user when the browser closes. I use onunload in javascript. Now, my problem is it always logout the user everytime the user navigates to other page. What I want is just to logout when the browser closes.
Here's my code:
<script type="text/javascript">
window.onbeforeunload = confirmExit;
window.onunload = logout;
function confirmExit() {
return "Are you sure you want to leave?";
}
function logout() {
window.location = '<?php echo WEBSITE_URL ?>?logout=true';
}
</script>
Hope someone will help me ASAP!
In your case its better to send a ajax request to do the task rather then redirecting the user to logout.
Also send it on onbeforeunload
Or a better idea will be add a cookie with user last activity. and check on every page load the if the last activity is more then the required limit server redirect to the login page clearing the session.
I don't think it's possible to detect in javascript when the window is closed. As you've found out onunload event doesn't have the information why the page is unloaded: if the user closes the window or navigates to a different page.
What is more, the user may navigate to a different page by typing the url in the address bar - you might want to 'log him out' in that case too.
The best way to achieve it is to have a timeout on your session. This will work event if the browser crashes.
Related
Differentiate browser/tab close and refresh event? I know this is not new question in stack overflow but still struggling out. Actually I need to logout user using server call if browser gets close.
I have tried tab count logic. But when I have only one tab left then its impossible to tell if its refresh or tab/browser close event .
If your goal is for the user to stay signed in for only the session, you can use session cookies to store the auth information. A session cookie is cleared when the website or browser is closed. See more information here.
I want to make a HTTP POST (or even GET is fine) request when the user leave the page.
I tried with 'onbeforeunload' 'unload' event listeners to watch when the users redirect to some other different page.
Is there any way I can check whether the user clicked on 'Leave' or 'Stay' button in default 'onbeforeunload' confirm box?
I want to call the function (make a request) only when he clicks the 'Leave' button.
How can I achieve this?
you can try
window.onunload
the function depends on browser
There is both window.onbeforeunload and window.onunload, which are used differently depending on the browser.Quote from here
onunload (or onbeforeunload) cannot redirect the user to another page. This is for security reasons.
If you want to show a prompt before the user leaves the page, use
onbeforeunload:
window.onbeforeunload = function(){
return 'Are you sure you want to leave?';
};
Or with jQuery:
$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});
This will just ask the user if they want to leave the page or not, you cannot redirect them if they select to stay on the page. If they select to leave, the browser will go where they told it to go.
You can use onunload to do stuff before the page is unloaded, but you cannot redirect from there (Chrome 14+ blocks alerts inside onunload):
window.onunload = function() {
alert('Bye.');
}
Or with jQuery:
$(window).unload(function(){
alert('Bye.');
});
Well, this is what I did :
When user tries to leave the page - Call the API (which I wanted to call if user clicks on leave button) within onbeforeunload event listener.
If user clicks on leave button, he will be redirected to other page.
If user clicks on stay button, I have a timeout function inside onbeforeunload event listener which will be executed after certain amount of time (2 seconds) where user would stay on current page itself. [In this API I'm revoking the operations what was done by API which was called in 1st step]
This question already has answers here:
JavaScript, browsers, window close - send an AJAX request or run a script on window closing
(9 answers)
Closed 6 years ago.
I am working on a website, where at a time one admin can login. I implemented this by saving value in database. Now the problem is when current admin forget to logout, the value in database do not change and another admin can't login again. Even this current admin can't login later on, cause the value in database is checked in condition.
I have looked in window.onbeforeload and window.onunload functions, but it only trigger on page refresh etc, not detecting browser close tab/window. I want to detect closing browser tab and call function upon close, so I can change value in database using ajax.
Any help will be appreciated! Thanks
The event you are looking for is onbeforeunload
Use window.uneforeunload which is trigged when tab/window is closed.
Updated
window.onbeforeunload = function (){
// update your database from here before page closed
}
DEMO
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript To Get An Alert When Closing The Browser Window
I am having a site on which users are visiting and uploading video, in that i am having a problem that users are closing the browsers or navigating from the page without letting the upload process to complete, i want to show a alert box to the user to not close the window or not to navigate from the page when uploading process is going on, i tried it through window.unload but it will not work for me because as the uploading process is going on the page will be submitted so that event will always be called, please tell me the way to show the alert box to users on window close or they navigate from the page.
Thanks in advance,
Ravinder Singh
Did you try to use:
window.onbeforeunload
If the user happens to have a delay in connecting to my site, the ajax hasnt timed out and the user decides to close the window. The ajax query will be terminated? Is there a way i can say still processing are you close you want to leave this page? (bonus if it will close once ajax was successful). How do i do this?
I am not sure if this is the same thing (maybe its built into firefox?) but when i closed this page it said
Are you sure you want to navigate away from this page?
You have started writing or editing a post.
Press OK to continue, or Cancel to stay on the current page.
I am positive i seen this other places. How do i make this appear when the user isnt submitting ajax and is in the middle of a post?
You can use the window.onbeforeunload event to handle this. Set a variable to false at the start of the ajax request. And in the callback function set its value to true, and in the window.onbeforeunload event check that variable and display suitable message.
Note
This will fire when you refresh your page also.
You can implement the onbeforeunload handler in js:
window.onbeforeunload = function()
{
if (showMessage)
{
return trye
}
else
{
return;
}
}