由於專案的需要,在頁面當使用者點擊後退鍵時,不允許頁面退到上一個頁面,由於純粹用js無法實現,所以用到了windows的宿主腳本語言wsh,這樣當使用者點擊鍵盤上的後退按鈕的時候就不會退到上一頁,但是點擊瀏覽器上的後退按鈕還是有效。 (wsh具體的內容網路上已經很多,這只是其中的一個應用而已)
具體的實作程式碼如下:
<script language=javascript>
function onbeforeunloadattachload()
{
if(pub_keycode==8)//如果按鍵是後退鍵則不允許退出
{
event.returnValue = ;
var wsh = new ActiveXObject(WScript.Shell);
wsh.sendKeys({ESC})
}
}
var pub_keycode;//取得遊標在頁面時,按鍵代碼
function onkeydownattachload()
{
pub_keycode=event.keyCode
}
document.attachEvent(onkeydown,onkeydownattachload);
window.attachEvent(onbeforeunload,onbeforeunloadattachload);
</script>