2015-11-04 70 views
0

注意:我看到Question但问题是不一样的。window.onbeforeunload只适用于我刷新页面

我有一个页面命名login_check.cfm而这个页面做一个location.href到home.cfm

的home.cfm我有这样的代码

<script> 
window.onbeforeunload = function() { window.history.go(0); }; 
</script> 

<script> 
window.onbeforeunload = function() { window.history.forward(1); }; 
</script> 

我的意图是禁止用户使用后退按钮,而其工作只能做一次刷新或者使用一个链接来做刷新。 问题是我的页面是基于90%ajax的,当用户点击后退按钮时,用户返回登录页面的时间最长。

使用Document ready()没有区别。

意见建议?

+0

洛尔 “QuestionsOverflow”:d – nicael

+0

我会建议使用'location.replace'代替'location.href'。 –

+0

我得到一个空白页使用此 – Deivi

回答

0

,我发现这个老Question其中rohit416提供一个良好的answer,它仍然有效

(function ($, global) { 

    var _hash = "!", 
    noBackPlease = function() { 
     global.location.href += "#"; 

     setTimeout(function() { 
      global.location.href += "!"; 
     }, 50); 
    }; 

    global.setInterval(function() { 
     if (global.location.hash != _hash) { 
      global.location.hash = _hash; 
     } 
    }, 100); 

    global.onload = function() { 
     noBackPlease(); 

     // disables backspace on page except on input fields and textarea. 
     $(document.body).keydown(function (e) { 
      var elm = e.target.nodeName.toLowerCase(); 
      if (e.which == 8 && elm !== 'input' && elm !== 'textarea') { 
       e.preventDefault(); 
      } 
      // stopping event bubbling up the DOM tree.. 
      e.stopPropagation(); 
     }); 
    } 

})(jQuery, window);