Window.onbeforeunload: is it possible to get any information about how the window was unloaded?

I am developing one of those warning windows that tells the user that they may have unsaved data, but I only need to warn them if they leave the page. He currently does this on updates, postbacks, etc. I was wondering if there is a way to tell how the page was uploaded or else to get more detailed information about what the user is doing to offload the page. (jquery solutions are welcome).

Code for reference:

    window.onbeforeunload = function () {
        if (formIsDirty) {            
            formIsDirty = false; 
            return "Are you sure you want to navigate away from this page?";
        }
    }
+5
source share
4 answers

on an beforeunloadevent that we can do below:

  • We can pass the event as a parameter to the function, as in the answer above. Now we can use this event for the available information attached to this event.
  • And we can access document level variables.

For example, it document.activeElementwill give you the last element that you clicked, which caused the page to load.

Hope this helps!

+6
source

Short answer: there is no easy way to find out what causes onbeforeunload to fire.

. window.onbeforeunload window.event, , , .

, window.event.srcElement , , onbeforeunload .

onbeforeunload MSDN .

: -

ASP, , "__EVENTTARGET". , ​​ ASP.

keyCode ( F5 , ) , , X ().

+2

, . , , onbeforeunload. , keydown , Enterkey. , onbeforeunload , .

_EVENTTARGET, jbabey. , , , .

, , , _EVENTARGET onbeforeunload, , .

.

            window.onbeforeunload = function (e) {
            if ($('[id$=__EVENTTARGET]').val().indexOf('btnValidateMaterials') != -1) {
                confirmExit = false;
            }

            if (DateOrQtyHasChanged() && confirmExit) {

                if (/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
                    var message = $('[id$=hfLeaveMessageFF]').val();
                    if (confirm(message)) {
                        history.go();
                    }
                    else {
                        window.setTimeout(function () {
                            window.stop();
                        }, 1);
                    }
                }
                else {
                    var message = $('[id$=hfLeaveMessage]').val();
                    return message;
                }
            }
        }
+2

, .

" " , .

document.getActiveElement , . , F5, , .

0

All Articles