2014-10-07 61 views
0

在我創建的網頁中,我想在頁面關閉時自動斷開連接,但它拒絕工作。斷開連接功能存在於頭部正確引用的外部.js文件中。嘗試使用window.onbeforeunload觸發功能

<script> 
    window.onbeforeunload = disconnect; 
</script> 

我之前將它從「disconnect();」改爲「 「斷開連接」。結果沒有變化。任何幫助,將不勝感激。

+0

'disconnect'的定義在哪裏? – hindmost 2014-10-07 13:55:47

+0

試試這個:** https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload** – myfunkyside 2014-10-07 14:01:19

+0

@myfunkyside那和OP一樣使用 – hindmost 2014-10-07 14:13:28

回答

0

因爲我不知道什麼是「斷開」的確,這裏是如何將功能綁定到window.onbeforeunload事件一個簡單的例子:

<script type="text/javascript"> 

    // bind a function to the window.onbeforeunload event 
    window.onbeforeunload = function(event) { 
     // your disconnect() function 
     disconnect(); 
     // bad ui, but for example - prompt user to verify they want to leave the page 
     prompt("Are you sure you want to leave this page?"); 
     // log to the developer tools console that this event fired 
     console.log("onbeforeunload event fired"); 
    }; 

</script> 

希望幫助。看起來問題可能在於你如何定義disconnect()