简体   繁体   English

JavaScript + onunload事件

[英]JavaScript + onunload event

I want to fire onunload event to do some clean up operations, I have multiple tabs(Navbar) showing multiple links to different web pages,my problem is that even when I'm in some other page my unload function which is in tag of some other jsp is fired. 我想触发onunload事件来执行一些清理操作,我有多个选项卡(导航栏)显示了指向不同网页的多个链接,我的问题是,即使在其他页面中,我的卸载功能也位于某些标签中其他jsp被解雇。 Please help to resove this, I want unload function to be called when user closes browser in that page. 请帮助解决此问题,我希望在用户关闭该页面中的浏览器时调用卸载功能。

I'm not sure how you got the onunload event to work....The problem I've found with using the onunload event is that it is fired after the page has been unloaded. 我不确定您如何使onunload事件正常工作。...使用onunload事件发现的问题是页面卸载后将其触发。 This means that no more JavaScript can be executed because the page has been unloaded. 这意味着无法再执行JavaScript,因为页面已被卸载。

You should be looking into using the onbeforeunload event. 您应该考虑使用onbeforeunload事件。 This event is a little unique because if the function that handles this event returns anything a pop up is displayed asking the user if they would like to continue with the operation. 该事件有点独特,因为如果处理该事件的函数返回任何内容,则会显示一个弹出窗口,询问用户是否要继续操作。 So, in your case make sure that your function doesn't return anything. 因此,请确保您的函数不返回任何内容。 The other thing to note about the onbeforeunload event is that, at this time, Opera does not support it (Safari, FireFox, and Internet Explorer do though). 关于onbeforeunload事件要注意的另一件事是,Opera目前不支持该事件(但是Safari,FireFox和Internet Explorer支持)。

Both the onbeforeunload and onunload events are executed every time the page is unloaded. 每次卸载页面时,都会执行onbeforeunload和onunload事件。 If a control on your page submits the page to the server code, the page is unloaded and the JavaScript is executed. 如果页面上的控件将该页面提交给服务器代码,则该页面将被卸载并执行JavaScript。

If you don't want the JavaScript to be executed when a control on your page is submitting it to the server you have to implement something that checks to see whether or not your code should be executed. 如果您不希望在页面上的控件将JavaScript提交到服务器时执行JavaScript,则必须实施一些检查以查看是否应执行代码。

This is simple, add a JavaScript boolean to the page and a function that set's this boolean to true. 这很简单,向页面添加一个JavaScript布尔值,并将此布尔值设置为true的函数。 Make sure that every element in your page that posts back to your server code sets this boolean to true before it submits the page. 确保页面中回发到服务器代码的每个元素在提交页面之前将此布尔值都设置为true。 Check this boolean in your onbeforeunload event to see if your cleanup code should be executed. 在onbeforeunload事件中检查此布尔值,以查看是否应执行清理代码。

Hope this helps, 希望这可以帮助,

-Frinny -弗林尼

It seems that the unload function has been created in a global scope. 似乎已在全局范围内创建了卸载功能。 Try placing that function only on the page you want to act. 尝试仅将该功能放在您要执行的页面上。

You have a frameset page? 您有框架集页面吗? And you want to be notified when they navigate away from the frameset? 您想在他们离开框架集时得到通知吗? Add an onbeforeunload on the frameset. 在框架集上添加onbeforeunload。 I don't know what you mean by clean up, but you can't send XHRs during unload safely across browsers 我不知道清理的含义,但是您无法在跨浏览器安全卸载期间发送XHR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM