Is there any way to handle page refresh in JavaScript ? After browser close button is clicked my application should be logged out and after clicking on browser back button application should be logged out also.
Try this snippet by clicking run and reload or backwards in your browser.
window.onbeforeunload = function(e) {
//call your function
return "Attention Reload"
};
Have a look at this eventHandler
Try This Answer
This answer will help you out
Also this only shows how to access the back button functionality in cross browser, However in the scope of those functions,
You can destroy your token or whatever to logout or you can directly use window.location.href, description of the same is given in Window.location.href
Hope that helps, Also if this does not solve specify more details in question that how are you logging out,
Thanks & Regards
Shohil Sethia
Related
so I want my app that when I close the tab is to clear the localStorage, so I tried this:
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
this.localStorage.clear()
e.returnValue = '';
});
But the problem is it also runs when I refresh the page so the items on my localStorage gets cleared even on refresh and now I have to re log in again, is there a function that only detects when I close the tab?
I don't think you can. There is very limited information about what goes on in a browser outside of your app. Many of these functionalities are available through particular APIs instead of being directly exposed.
I suggest using session storage as this is its default behaviour and intended use.
This remind me that you can only know that a 'POP' action was dispatched when a user clicks the back or forward buttons. You cannot know which one of them was pressed though.
I have a webforms web app, and all I need to do is, when a user clicks on the browser back button, I want to make a post-back or reload my page so it makes the post-back itself so new data is loaded.
I am using a library called jQuery-backDetect
which allows me to detect browser's back button click and I try to do is to make a post-back using __doPostback('arg1', 'arg2'). But it won't work at all.
And the strangest thing here is that, if I have the browser's debugger (in developer tools) open, or I pause the code execution using a breakpoint, it works perfectly. Here is a simple code I have written:
$(window).load(function(){
$('body').backDetect(function(){
// Callback function
debugger;
__doPostback('arg1', 'arg2');
});
});
I have tried to go through all the question here but they didn't help.
As strange as the question sounds, I really hope someone can help me. Thanks
You might consider wrapping the page content in an UpdatePanel, that might help create the behavior you are looking for.
I seen many post regarding same problem but i am not getting exact solution. i want to delete cookie on browser or tab close event using javascript. I have made delete cookie function and called on onbeforeunload event. But i seen that event also called when page refresh i dont want to delete cookie on page refresh. And i seen in many post that they are detecting link click, keypress event of F5 and form submit and in that they preventing onbeforeunload event. But then what about refresh button click and press enter at url bar. So i think this is not a exact solution. so help me out from this problem.
Further information is i am creating cookie using PHP and want to delete this cookie on browser close.
Cookies are automatically deleted when the browser is closed, unless you specify a lifetime for the cookie.
If you want to delete a cookie on browser close, better would be to check if cookie exists on page load and delete that.
To delete all the cookies when browser close uses the following code
$(window).bind('beforeunload', function(event) {
var cookies = $.cookie();
for(var cookie in cookies) {
$.removeCookie(cookie);
}
return true;
});
Hope this will solve your problem.
I want to set a cookie when a visitor on the page closes the browser.
I used onbeforeunload method like this
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>
followed this link
But found out that even refresh of page or click on any hyper link on the page,pops up an alert.
I need to set cookie only when visitor clicks cross button of browser and not on page refresh.
I followed some of the links like the one above and another here. And found from the post that we can not differentiate between close and refresh. But those posts were 3-4 years back.
Is there is any way to differentiate these events using JavaScript or Jquery?
I'm afraid you have no way to tell.
The only thing you can do is overload clicks on links and set a flag. But you can never know if it is a refresh or a close...
Hmm, what about this:
Set a cookie onbeforeunload
Globally onload, check the timestamp of the cookie to see whether this was a link, or a new session
If the timestamp difference is only a few seconds, delete the cookie
Well, unload is called when browser is closed and onload is called when you try to reload. You can use these properties and set flags , and fire events accordingly
Browser close and reload Check this link for more details
I have a strange problem, I have a logout link that displays only if the user is logged in. I have this code inside the the onclick event of this link (using a function):
FB.logout();
window.location = 'http://www.google.com';
If I click on this once, nothing happens, I know that the function gets executed because I've tested this with an alert. However, if I click on it a second time, the page reloads itself and the user is indeed logged out. The page is never directed to google.com, so the function never gets as far as the window.location part.
I have the window.location because I thought that if I could refresh the page using window.location.refresh once the logout is complete that it would successfully log the user out. However, if anybody has any other techniques on how to logout the user out of facebook, I would love to hear them! I've attempted to manually delete the cookie, but that didn't work, the cookie still existed for some reason. I've also tried this:
FB.logout(function(response) {
window.location = 'http://www.google.com';
});
I know a callback like this is possible because of the documentation:
http://developers.facebook.com/docs/reference/javascript/FB.logout
Has anybody else had this issue before? Any advice would help thanks!
UPDATE: After some debugging I've found out that if I click the link once. Then manually refresh the page it logs the user out.
From your description it looks to me that logout is working properly, but page refresh doesn't.
Try something like this maybe:
FB.logout(function(response) {
window.location.reload(true);
});
You might try putting an alert in the callback function to see if FB.logout is really completely successfully.
I was originally putting FB.logout directly in the onclick event of an anchor link and while that worked in FF and Chrome it did not work in IE or in my Android browser.
FF and Chrome will execute the FB.logout call quickly enough for this to work properly but that IE and mobile browsers (because of network speed in addition to differences in the JavaScript engine) will not complete the call successfully before the browser loads whatever page you're redirecting to.
I think I am trying to do the exact same thing you are doing and this code worked for me:
function mysignout(url)
{
FB.logout(function()
{
top.location.href = 'url';
});
}
Surprisingly, it take 2+ seconds for FB.logout to completely successfully in most environments. There is obviously some kind of ajax call involved to revoke authentication on the server, not just destroying the local cookie.