jquery - How can I detect browser tab refresh or close using javascript -
this question has reply here:
identifying between refresh , close browser actions 6 answersi have 1 problem, have javascript
function want utilize when browser closed, how can observe a browser beingness closed
?
i have made research on got solution onunload
or beforeunload
both functions beingness executed when reloading page, there solution can differentiate reload , browser/tab close.
answer is,
</head> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script type="text/javascript" language="javascript"> var validnavigation = false; function endsession() { // browser or broswer tab closed // sth here ... alert("bye"); } function wireupevents() { /* * list of events triggers onbeforeunload on ie * check http://msdn.microsoft.com/en-us/library/ms536907(vs.85).aspx */ window.onbeforeunload = function() { if (!validnavigation) { endsession(); } } // attach event keypress exclude f5 refresh $(document).bind('keypress', function(e) { if (e.keycode == 116){ validnavigation = true; } }); // attach event click links in page $("a").bind("click", function() { validnavigation = true; }); // attach event submit forms in page $("form").bind("submit", function() { validnavigation = true; }); // attach event click inputs in page $("input[type=submit]").bind("click", function() { validnavigation = true; }); } // wire events dom tree ready $(document).ready(function() { wireupevents(); }); </script> </head> <body> <h1>eureka!</h1> <a href="http://www.google.com">google</a> <a href="http://www.yahoo.com">yahoo</a> </body> </html>
javascript jquery html
No comments:
Post a Comment