2010-05-17 58 views
1

我有一個JavaScript類的方法和屬性。Javascript類使用window.onunload事件

是否有可能將此類連接到pageunload事件?

我想在用戶移動頁面時在我的課程中調用方法。

我該怎麼做。

感謝

+1

這取決於你想做什麼,卸載事件充其量是不可靠的,你可以發佈你想要採取的行動嗎? – 2010-05-17 11:17:44

+0

嗨,尼克,我需要問用戶是否需要保存他在各種控件中輸入的信息。現在警報會做。 – SetiSeeker 2010-05-17 11:18:48

+0

+1 @ Nick。根據我的經驗,卸載事件是討厭的生物。 – Anthony 2010-05-17 12:58:07

回答

0

onbeforeunload事件處理函數返回一個字符串,將顯示一個對話框。

<html> 
<head> 
</head> 
<body> 
test 
<script> 
window.onbeforeunload = function() { 
    return "*** Warning: changes are not saved! ***"; 
}; 
</script> 
</body> 
</html> 

在Internet Explorer中該對話框看起來是這樣的:

--------------------------- 
Windows Internet Explorer 
--------------------------- 
Are you sure you want to navigate away from this page? 

*** Warning: changes are not saved! *** 

Press OK to continue, or Cancel to stay on the current page. 
--------------------------- 
OK Cancel 
--------------------------- 

你的文本將一些瀏覽器特定的文本被夾,但是這是唯一的出路。

+0

如何在類中響應卸載事件的類中執行此操作? – SetiSeeker 2010-05-17 12:00:09

+0

你能告訴我你是如何創建你的「班級」?你使用一個框架,或者只是一個函數閉包或一個具有屬性的對象?爲你的問題添加一些示例代碼,我將在我的答案中使用它。 – 2010-05-18 09:24:49