简体   繁体   中英

How to handle for browser refresh, back button

  • I'm developing a site for internet banking system, once logged in I have main page(session is maintain here) and few tabs/links as child for the main page,

  • I already have js to kill the session or to logout, but the problem is with the event handler

  • when the page is refreshed or back button is click, I have to handle as below

     window.onbeforeunload = function() { if( confirm("This page will be close") ){ //called the logout fn to kill session } } 
  • the above js works fine, but the problem with the above code is that its called whenever other links/tabs are clicked in the main page, so its keep on logout

  • I'm sorry if my question is not clear or duplicate, do anyone can suggest the way to handle this

  • for your info, I'm using jsp and servlet with js, this project cannot be used jquery and cannot disable back button and refresh button

Thanks in advance!

  • You can save previously visited URL (information) as a hashed input tag in next page, so whenever you are going to visit it again you can understand that back button has been pressed.

  • Another solution would be using document.referrer .

  • You can also use a plugin with JQuery named History . It is available at https://github.com/browserstate/History.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM