[removed] not working in chrome

后端 未结 9 704
遥遥无期
遥遥无期 2020-11-27 07:25

This is the code which i used for window.onbeforeunload......




        
相关标签:
9条回答
  • 2020-11-27 08:15

    You should try this:

    window.onbeforeunload = function(e) {
      e.returnValue = 'onbeforeunload';
      return 'onbeforeunload';
    };
    

    This works on latest Chrome. We had the same issue the e.returnValue with value of onbeforeunload solved my problem.

    Your code should be like this:

        <head>
        <script>
    
        window.onbeforeunload = function(e) {
            e.returnValue = 'onbeforeunload';
            func();
            return ''onbeforeunload'';
        };
    
        function func() 
        {
            var request = new XMLHttpRequest();
            request.open("POST", "exit.php", true); 
            request.onreadystatechange = stateChanged;
            request.send(null);
        }
        function stateChanged()
        {
            if (request.readyState == 4 || request.readyState == "complete")
                alert("Succes!");
        }
        </script>
        </head>
    
    0 讨论(0)
  • 2020-11-27 08:15

    Current versions of Chrome require setting the event's returnValue property. Simply returning a string from the event handler won't trigger the alert.

    addEventListener('beforeunload', function(event) {
      event.returnValue = 'You have unsaved changes.';
    });
    
    0 讨论(0)
  • 2020-11-27 08:16

    I'm running Chrome on MacOS High Sierra and have an Angular 6 project whithin I handle the window.beforeunload an window.onbeforeunload events. You can do that, it's worked for me :

    handleUnload(event) {
      // Chrome
      event.returnValue = true;
    }
    

    It show me an error when I try to put a string in event.returnValue, it want a boolean.

    0 讨论(0)
  • 2020-11-27 08:17

    None of the above worked for me. I was sending a message from the content script -> background script in the before unload event function. What did work was when I set persistent to true (in fact you can just remove the line altogether) in the manifest:

    "background": {
        "scripts": [
            "background.js"
        ],
        "persistent": true
      },
    

    The logic is explained at this SO question here.

    0 讨论(0)
  • 2020-11-27 08:22

    Know I'm late to this, but was scratching my head why my custom beforeunload message wasn't working in Chrome and was reading this. So in case anyone else does the same, Chrome from Version 51 onwards no longer supports custom messages on beforeunload. Apparently it's because the feature has been misused by various scams. Instead you get a predefined Chrome message which may or may not suit your purposes. More details at:

    https://developers.google.com/web/updates/2016/04/chrome-51-deprecations?hl=en#remove-custom-messages-in-onbeforeload-dialogs

    Personally do not think the message they've chosen is a great one as it mentions leaving the site and one of the most common legitimate uses for onbeforeunload is for dirty flag processing/checking on a web form so it's not a great wording as a lot of the time the user will still be on your site, just have clicked the cancel or reload button by mistake.

    0 讨论(0)
  • 2020-11-27 08:25

    Confirmed this behavior on chrome 21.0.1180.79

    this seems to work with the same restritions as XSS, if you are refreshing the page or open a page on same domain+port the the script is executed, otherwise it will only be executed if you are returning a string (or similar) and a dialog will be shown asking the user if he wants to leans or stay in the page.

    this is an incredible stupid thing to do, because onunload/onbeforeunload are not only used to ask/prevent page changes.

    In my case i was using it too save some changes done during page edition and i dont want to prevent the user from changing the page (at least chrome should respect a returning true or change the page without the asking if the return is not a string), script running time restrictions would be enought.

    This is specially annoying in chrome because onblur event is not sent to editing elements when unloading a page, chrome simply igores the curent page and jumps to another. So the only change of saving the changes was the unload process and it now can't be done without the STUPID question if the user wants to change it... of course he wants and I didnt want to prevent that...

    hope chrome resolves this in a more elegant way soon.

    0 讨论(0)
提交回复
热议问题