最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.onbeforeunload fires in IE for partial postback events - Stack Overflow

programmeradmin1浏览0评论

I want to warn users when the refresh or press back button of the browser if they have entered some thing in text boxes. So I have used window.onbeforeunload function to do it.

window.onbeforeunload = function () {
    if (if user hasn't entered any thing) {
        return;
    }
    return 'Entered data will be lost.';
};

this javascript code works fine in Firefox and chrome. But in IE this function fires for buttons with partial post backs. Any solution to overe this problem in IE ? Thanks :)

  • It's happen because of Bug in IE

I want to warn users when the refresh or press back button of the browser if they have entered some thing in text boxes. So I have used window.onbeforeunload function to do it.

window.onbeforeunload = function () {
    if (if user hasn't entered any thing) {
        return;
    }
    return 'Entered data will be lost.';
};

this javascript code works fine in Firefox and chrome. But in IE this function fires for buttons with partial post backs. Any solution to overe this problem in IE ? Thanks :)

  • It's happen because of Bug in IE
Share Improve this question edited Feb 21, 2016 at 2:58 cp100 asked Feb 13, 2013 at 6:03 cp100cp100 1,5036 gold badges20 silver badges36 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

window.onbeforeunload function not execute in IE correctly. Because some partial postbacks are also considered as "Trying to leave the page". If a link on the page has href="javascript:...." (ASP.Net LinkButton is rendered like this), IE will incorrectly fire the window unload event when the link is clicked. so, I kept a dirtyflag variable and added

var __ignoreDirtyFlag = false;

$(document).ready(function () {
    if ($.browser.msie) {
        $('[href^="javascript:"]').bind('click.ignoreDirtyFlag', function () {
            __ignoreDirtyFlag = true;
        });
    }
}); 

now when page is loaded, for links started with href^="javascript:" will assigned that function. when link is clicked then it make __ingoreDirtyFlag variable true; then added

window.onbeforeunload = function globalWindowUnload() {
    if (!__ignoreDirtyFlag && isDataFilled == true) {
        return "You have unsaved changes on this page. If you leave this page, those changes will be lost.";
    }
    __ignoreDirtyFlag = false;
    return;
};

if there is any asp update panels you have used in your page, above function binding to the link will be removed. so we have to bind function to the links again when update panel get refreshed.

so,

function foo() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
}

function endRequestHandler(sender, args) {
    // Do your stuff
    if ($.browser.msie) {
        $('[href^="javascript:"]').bind('click.ignoreDirtyFlag', function () {
            __ignoreDirtyFlag = true;
        });
    }
}

and

for body tag onload method, I assinged foo() method. <body onload="foo()">

this hack is working fine with IE and all the browsers :) thanks

`var inFormOrLink = false; var url_auth = ''; var __ignoreDirtyFlag = false;

document).ready(function () {

if ($.browser.msie) {
    $('').bind('click.ignoreDirtyFlag', function () { __ignoreDirtyFlag = true; alert("teste luiz") });

    window.onbeforeunload = function () {
        if (!__ignoreDirtyFlag) {
            return "Tem certeza que deseja encerrar a sessão TESTE LUIZ?";
        }
        __ignoreDirtyFlag = false;
        return;
    };
}
else {
    $('*').live('click', function () { inFormOrLink = true; alert("Passou aqui 1 LUIZ"); });
    $('*').bind('submit', function () { inFormOrLink = true; alert("Passou aqui 2"); });
    window.onbeforeunload = function () {
        if (!inFormOrLink) {

            if (window.location.href.indexOf("DEV2") != -1) {
                $.post("/DEV2/SSQ/AUTH/LOGOUT", null);
                return 'Tem certeza que deseja encerrar a sessão TESTE DEV2?';
            }`

Purpose is to logout the user whenever he closes the browser or manually change the destination url

var url_auth = '';var __ignoreDirtyFlag = false;$(document).ready(function () {if ($.browser.msie) {$('a').live('click', function () { __ignoreDirtyFlag = true;}); $('*').bind('submit', function () { __ignoreDirtyFlag = true; });window.onbeforeunload = function () {if (!__ignoreDirtyFlag) {if (window.location.href.indexOf("DEV2") != -1) {$.post("/DEV2/SSQ/AUTH/LOGOUT", null);return 'Tem certeza que deseja encerrar a sessão ?';}}};
发布评论

评论列表(0)

  1. 暂无评论