简体   繁体   中英

Show error on pressing back/forward button

我有一个登录页面,当登录的用户按下后退按钮时,该页面应该显示错误而不是显示登录页面,并且应该在前进按钮上应用相同的内容。

window.onbeforeunload event will fire when the user leaves the page.

var prevURL = document.referrer;

window.onbeforeunload = function() {
  if(prevURL == 'provide your login page URL here') {
    return "Error message";  // alert
    //or do whatever you want
  }
};

Note that this event will fire when the user leaves the page for anyway.

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