2010-01-08 53 views
4

當用戶點擊另一個鏈接或刷新頁面時,窗口會卸載,如何防止/確認呢?如何停止窗口卸載?

+0

請注意此功能。除非你防止人們意外離開可編輯的上下文,否則人們會恨你。 – 2010-01-08 17:52:26

回答

3

我認爲onbeforeunload事件處理程序將是你在找什麼。

嘗試以下操作:

window.onbeforeunload = confirmExit; 
    function confirmExit() 
    { 
    return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; 
    } 
0

看看卸載這個page的底部。

+1

你能爲此答案添加一些內容嗎?想象一下鏈接不存在,這會有多有用? – Sampson 2010-01-08 17:30:51

+0

@Jonathan Sampson - 好點。我會在下一次做。我會更新這個答案,但接受的答案基本相同。 – 2010-01-08 18:46:08