2010-05-17 36 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