javascript 后退按钮重定向脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30990027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:04:44  来源:igfitidea点击:

Back button redirect script

javascripthttp-redirect

提问by Alex

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.

我正在尝试开发一个 javascript 代码,当用户点击网页的后退按钮时,它可以被重定向到另一个 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();

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

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

<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>

回答by Alex

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

如果其他人正在寻找答案,我已经找到了答案。

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

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

(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");
    },10);
  }
}, false);
}(window, location));

and then include this code in your HTML file:

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

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

so that it calls the history-stealer.js file

以便它调用 history-stealer.js 文件

回答by Corey Young

Well if you want to use the browser back button I would push each page change into the History Api

好吧,如果您想使用浏览器后退按钮,我会将每个页面更改推送到 History Api

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_historyhttp://www.sitepoint.com/javascript-history-pushstate/

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history http://www.sitepoint.com/javascript-history-pushstate/

what you would need to do is while the user navigates your page, whenever they do something that you feel should be put into their history you use

您需要做的是,当用户浏览您的页面时,无论何时他们做了一些您认为应该放入您使用的历史记录中的事情

history.pushState({}, 'Title: Google', 'http://www.google.com/');

Now when the user clicks the back button the previous page will be google.com, though I'm sure you want it be a certain end point on your webpage.

现在,当用户单击后退按钮时,上一页将是 google.com,但我确定您希望它是您网页上的某个端点。