Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.onbeforeunload: Is it possible to get any details about how the window was unloaded?

I'm developing one of those warning windows that tells the user that they may have unsaved data, but I only need it to warn them if they're leaving the page. Currently it does so on refreshes, postbacks, etc. I was wondering if there was any way to tell how the page was unloaded or otherwise get more details about what the user is doing to unload the page. (jquery solutions welcome).

Code for reference:

    window.onbeforeunload = function () {
        if (formIsDirty) {            
            formIsDirty = false; 
            return "Are you sure you want to navigate away from this page?";
        }
    }
like image 608
Maxx Avatar asked Dec 20 '11 20:12

Maxx


People also ask

What is window unload?

onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.). Note: The onunload event is also triggered when a user reloads the page (and the onload event).

What is the difference between Onbeforeunload and Onunload?

onbeforeunload Below are my findings on the iPad; Using window. onunload , I am able to get an alert when user navigates to a different page from myPage. html (either by clicking on some link or doing a Google search while on myPage.

Where do you put Windows Onbeforeunload?

Note: If the onbeforeunload event is not assigned to the <body> element, you must assign/attach the event on the window object, and use the returnValue property to create a custom message (see syntax examples below).

How do you unload a page?

unload() . The unload event occurs when the user navigates away from the page. The unload event is triggered when: a link to leave the page is clicked.


1 Answers

on beforeunload event we can do below things:

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

For example document.activeElement will give you the last element you clicked that caused the page unload.

Hope this helps!!

like image 56
Shri Avatar answered Sep 29 '22 13:09

Shri