2014-07-04 136 views
0

我希望頁面刷新瀏覽器歷史記錄返回按鈕時重新加載。但是,由於URL經常使用JavaScript的window.history.pushState進行更改,我不想在每次更改location時都重新加載頁面。默認情況下,瀏覽器只是更改網址,不會在點擊後退按鈕時重新加載頁面。刷新頁面上的瀏覽器歷史記錄返回按鈕

(通過這個我想用瀏覽器歷史記錄後退按鈕爲某種「撤消」功能。)

+0

也許HTML5可以幫助你,看的是[http://stackoverflow.com/questions/17507091/replacestate-vs-pushstate][1] [1]:http://stackoverflow.com/questions/17507091/replacestate-vs-pushstate – Cattla

+0

感謝您的評論。但是,使用'replaceState()'而不是'pushState()'殺死「撤消」功能。 – Daniel

回答

0

試試這個。

function HandleBackFunctionality(){ 
    //For IE 
    if(window.event){ 
     //To check if its Back 
     if(window.event.clientX < 40 && window.event.clientY < 0){ 
      alert("Browser back button is clicked…"); 
     //Its Refresh 
     } else { 
      alert("Browser refresh button is clicked…"); 
     } 
    } 
    //Other browsers 
    else { 
     if(event.currentTarget.performance.navigation.type == 1){ 
      alert("Browser refresh button is clicked…"); 
     } 
     if(event.currentTarget.performance.navigation.type == 2){ 
      alert("Browser back button is clicked…"); 
     } 
    } 
} 

您需要在onbeforeunload事件中調用此方法。

<body onbeforeunload="HandleBackFunctionality()"> 

但是,此方法不適用於鍵盤控件。

+0

控制檯中的任何錯誤? – Prashant

+0

在Firefox的控制檯中給我一個「ReferenceError:event is not defined」。在Chrome中沒有錯誤,但後退按鈕仍不會刷新。 – Daniel