简体   繁体   English

在javascript中处理IE窗口关闭事件

[英]Handle IE window close event in javascript


I need to handle when Internet Explorer (6, 8 and 9) window is closed, in order to ask his confirmation on it. 我需要处理Internet Explorer(6、8和9)窗口关闭时的问题,以便询问他的确认。 I tried the unbeforeunload , but, as you might know, it's triggered also when any link is clicked, oa form is submited. 我尝试了unbeforeunload ,但是,您可能知道,单击任何链接并unbeforeunload oa表单时也会触发该事件。 Some can say to use event mouse coordinates, but there are several ways to break such a validation (Alt+F4 for ex.). 有人可以说使用事件鼠标坐标,但是有几种方法可以打破这种验证(例如Alt + F4)。 There's also a way to set some variable to true when clicking links and check if in the event, but that does not work neither, as I got lots of various links on my pages, and the event is triggered multiple times in some cases. 还有一种方法可以在单击链接并检查事件时是否将某些变量设置为true,但是这也不起作用,因为我的页面上有很多各种链接,并且在某些情况下会多次触发该事件。
Also I tried to solve the problem using frames, like: make 2 frames, put my pages to one frame, and to the other (with zero size) put a page with onbeforeunload handler. 我也尝试使用框架来解决问题,例如:制作2个框架,将我的页面放到一个框架中,将另一个页面(具有零尺寸)放到带有onbeforeunload处理程序的页面中。 That would work just fine, but we work with a set of environment js, that we can't prevent to download, and those scripts remove any frames on the page, putting the entire page in the main window - fail… 那会很好,但是我们使用了一组环境js,我们无法阻止其下载,这些脚本会删除页面上的所有框架,从而将整个页面置于主窗口中-失败…
Can anybody, please, suggest anything else? 有人可以提出其他建议吗?

If you are going to use onbeforeunload (and it sounds right), you should have a flag that is set so that the event only bothers asking if there is a reason to stay on the page. 如果要使用onbeforeunload(听起来很正确),则应该设置一个标志,这样该事件才麻烦您问是否有理由留在页面上。

var hasChanges = false;

window.onbeforeunload = function() {

    if (hasChanges) {

        return "You have changes that will be lost if you continue leaving the page";

    };

};

Then you just have to set the variable to true ( hasChanges = true ) if there is a reason to stay on the page...so if nothing has changed, clicking your other links will not trigger the dialog. 然后,如果有理由停留在页面上,则只需将变量设置为true( hasChanges = true )...因此,如果没有任何更改,单击其他链接将不会触发对话框。

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

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