Capturing [removed]

前端 未结 7 1299
逝去的感伤
逝去的感伤 2020-11-28 08:58

I have a form where the input fields are saved onChange. In Firefox (5) this works even when the window is closed, but for Chrome and IE it doesn\'t and I need

7条回答
  •  借酒劲吻你
    2020-11-28 09:45

    You have to return from the onbeforeunload:

    window.onbeforeunload = function() {
        saveFormData();
        return null;
    }
    
    function saveFormData() {
        console.log('saved');
    }
    

    UPDATE

    as per comments, alert does not seem to be working on newer versions anymore, anything else goes :)

    FROM MDN

    Since 25 May 2011, the HTML5 specification states that calls to window.showModalDialog(), window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event.

    It is also suggested to use this through the addEventListener interface:

    You can and should handle this event through window.addEventListener() and the beforeunload event.

    The updated code will now look like this:

    window.addEventListener("beforeunload", function (e) {
      saveFormData();
    
      (e || window.event).returnValue = null;
      return null;
    });
    

提交回复
热议问题