后退按钮重定向脚本 [英] Back button redirect script

查看:227
本文介绍了后退按钮重定向脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个javascript代码,当用户点击网页的后退按钮时,可以将其重定向到另一个网址。

I am trying to develop a javascript code that when a user clicks on the back button of a webpage, it can be redirected to another URL.

我找到了这些代码行,但我想知道是否有更清洁和更有效的东西:

I have found these lines of code but I would like to know if there is something cleaner and more effective:

var bajb_backdetect={Version:'1.0.0',Description:'Back Button Detection',Browser:{IE:!!(window.attachEvent&amp;&amp;!window.opera),Safari:navigator.userAgent.indexOf('Apple')&gt;-1,Opera:!!window.opera},FrameLoaded:0,FrameTry:0,FrameTimeout:null,OnBack:function(){alert('Back Button Clicked')},BAJBFrame:function(){var BAJBOnBack=document.getElementById('BAJBOnBack');if(bajb_backdetect.FrameLoaded&gt;1){if(bajb_backdetect.FrameLoaded==2){bajb_backdetect.OnBack();}}bajb_backdetect.FrameLoaded++;if(bajb_backdetect.FrameLoaded==1){if(bajb_backdetect.Browser.IE){bajb_backdetect.SetupFrames()}else{bajb_backdetect.FrameTimeout=setTimeout("bajb_backdetect.SetupFrames();",700)}}},SetupFrames:function(){clearTimeout(bajb_backdetect.FrameTimeout);var BBiFrame=document.getElementById('BAJBOnBack');var checkVar=BBiFrame.src.substr(-11,11);if(bajb_backdetect.FrameLoaded==1&amp;&amp;checkVar!="HistoryLoad"){BBiFrame.src="blank.html?HistoryLoad"}else{if(bajb_backdetect.FrameTry&lt;2&amp;&amp;checkVar!="HistoryLoad"){bajb_backdetect.FrameTry++;bajb_backdetect.FrameTimeout=setTimeout("bajb_backdetect.SetupFrames();",700)}}},SafariHash:'false',Safari:function(){if(bajb_backdetect.SafariHash=='false'){if(window.location.hash=='#b'){bajb_backdetect.SafariHash='true'}else{window.location.hash='#b'}setTimeout("bajb_backdetect.Safari();",100)}else if(bajb_backdetect.SafariHash=='true'){if(window.location.hash==''){bajb_backdetect.SafariHash='back';bajb_backdetect.OnBack();}else{setTimeout("bajb_backdetect.Safari();",100)}}},Initialise:function(){if(bajb_backdetect.Browser.Safari){setTimeout("bajb_backdetect.Safari();",600)}else{document.write('<iframe id="BAJBOnBack" width="320" height="240" style="display: none;" src="blank.html" onunload="alert(\'de\')" onload="bajb_backdetect.BAJBFrame();"></iframe>')}}};bajb_backdetect.Initialise();

这就是你在HTML文档中使用它的方式:

and this is how you would use it in your HTML document:

<script type="text/javascript" src="js/backfix.min.js"></script>
<script type="text/javascript">// <![CDATA[
bajb_backdetect.OnBack = function(){
    document.location.href = 'http://google.com'; //Change the url here with your desired URL
}
// ]]></script>


推荐答案

我找到了答案以防其他人正在寻找对于它。

I have found the answer in case someone else is looking for it.

使用以下代码创建一个名为history-stealer.js的单独文件:

Create a separate file called history-stealer.js with the following code:

(function(window, location) {
history.replaceState(null, document.title, location.pathname+"#!/history");
history.pushState(null, document.title, location.pathname);

window.addEventListener("popstate", function() {
  if(location.hash === "#!/history") {
    history.replaceState(null, document.title, location.pathname);
    setTimeout(function(){
      location.replace("http://www.url.com");
    },0);
  }
}, false);
}(window, location));

然后在HTML文件中包含此代码:

and then include this code in your HTML file:

<script src="history-stealer.js"></script>

以便调用history-stealer.js文件

so that it calls the history-stealer.js file

这篇关于后退按钮重定向脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆