开发者

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. I tried the unbeforeunload, but, as you might know, it's triggered also when any link is clicked, o a form is submited. Some can say to use event mouse coordinates, but there are several ways to break such a validation (Alt+F4 for ex.). 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.

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. 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…

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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜