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

javascript - How to prevent window.onbeforeunload event being triggered by button and links events - Stack Overflow

programmeradmin3浏览0评论

My form contains multiple buttons and hyperlinks and i want to show alert on back button of browser. I have used below code which works fine on Chrome and Safari but not working in IE and Firefox:

       window.onload = function() {

            var btnClicks = document.getElementsByClassName('noPopup');
            var links = document.getElementsByTagName('a');

            for (var i = 0; i < links.length; i++) {
                links[i].onclick = setGlobal;
            }
            for (var i = 0; i < btnClicks.length; i++) {
                btnClicks[i].onclick = setGlobal;
            }
            function setGlobal() {
                window.btn_clicked = true;
                window.linkClicked = true;
            }

              window.onbeforeunload = function() {
                  if (!window.btn_clicked || !window.linkClicked) {
                      return 'Would you like to save first.';
                  }
              };

      };

My form contains multiple buttons and hyperlinks and i want to show alert on back button of browser. I have used below code which works fine on Chrome and Safari but not working in IE and Firefox:

       window.onload = function() {

            var btnClicks = document.getElementsByClassName('noPopup');
            var links = document.getElementsByTagName('a');

            for (var i = 0; i < links.length; i++) {
                links[i].onclick = setGlobal;
            }
            for (var i = 0; i < btnClicks.length; i++) {
                btnClicks[i].onclick = setGlobal;
            }
            function setGlobal() {
                window.btn_clicked = true;
                window.linkClicked = true;
            }

              window.onbeforeunload = function() {
                  if (!window.btn_clicked || !window.linkClicked) {
                      return 'Would you like to save first.';
                  }
              };

      };
Share Improve this question edited Dec 11, 2018 at 15:53 Kiquenet 15k36 gold badges151 silver badges247 bronze badges asked Jul 24, 2013 at 10:34 HBKHBK 1291 gold badge1 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

in IE - if you don't want the onbeforeunload to trigger - all you need to do is to set this event to null, like this:

window.onbeforeunload = null;

so what you would have to do is - add a click event listener to all your buttons and links (which redirect user to a different URI) and inside that listener - set the onbeforeunload event listener to null once they are clicked (before proceeding with the normal link/button action. Otherwise - set it to function (like you have in your code).

e.g.:

document.getElementById('myLink').onclick = function () {
    window.onbeforeunload = null;
};

Using this beforeunload will occur only on browser close neither occur on refresh of any click of anchor and also it will act as unload event by excluding return for hiding built-in popup message on before unload event


    function confirmExit()
    {
        $.ajax({
            type: "POST",
            url: site_url + 'wele_login/logout',
            success: function (data)
            {
            }
        });
    }
    $(window).on('mouseover', (function () {
        window.onbeforeunload = null;
    }));
    $(window).on('mouseout', (function () {
        window.onbeforeunload = confirmExit;
    }));

Mouse Out Occurs on Closing Browser window

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论