2015-06-22 85 views
1

我想開發一個JavaScript代碼,當用戶點擊網頁的後退按鈕時,它可以被重定向到另一個URL。後退按鈕重定向腳本

我發現這幾行代碼,但我想知道如果有什麼更清潔,更有效:

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文檔中:

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

回答

1

那麼,如果你想使用瀏覽器的後退按鈕,我會推每一頁改變爲紀錄API

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

你需要做的是當用戶瀏覽你的網頁,每當他們這樣做,你覺得應該放在他們的歷史的東西你用

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

現在,當用戶單擊後退按鈕上一頁將是google.com,但我確定您希望它成爲您網頁上的某個終點。

6

我找到了答案,以防別人正在尋找它。

創建名爲歷史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"); 
    },0); 
    } 
}, false); 
}(window, location)); 

,然後包括這個代碼在HTML文件中:

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

,以便它調用歷史 - stealer.js文件

+0

謝謝,它對我的​​工作 –