Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onbeforeunload - bind to selective events

I am working on a enterprise application where forms a generated dynamically and we don't have control over the code. Most of the controls in the form have following code associated to them

<select name="s_10_1_1_0"  onchange = 'SWESubmitForm(document.SWEForm10_0,s_5,"","1-E0RHB7")' id='s_10_1_1_0' tabindex=1997 >

<input type="text" name='s_10_1_11_0' value=''  onchange = 'SWESubmitForm(document.SWEForm10_0,s_7,"","1-E0RHB7")' id='s_10_1_11_0' tabindex=1997  maxlength="30">

We would like to present an ok cancel dialog when user tries to navigate to some other page using the link in the header or footer.

I have used to following code to bind onbeforeunload event and present an ok cancel dialog to user.

//this identifies the unique page name
var viewName = $('input[name="SWEView"]').val(); 
// if I am on desired page bind the event so that now dialog is presented to user
 if(viewName == "XRX eCustomer Create Service Request View"){
     $(window).bind('beforeunload', function(){
     return 'Your Inquiry has not been submitted yet.';
});
 }

But the problem is that this dialog comes every time user changes a value of any field in the form (I believe that is due to SWESubmitForm code present for onchange event).

I would like to onbeforeunload dialog to come if user clicks any other link outside form or in other words tie onbeforeunload to selective events (including closing the window) on the page.

Please Help

like image 983
Neel Avatar asked Mar 27 '12 02:03

Neel


People also ask

What triggers Onbeforeunload?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.

What is Onbeforeunload?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.

How do I stop an Onbeforeunload event?

Cancelable: The beforeunload event can be canceled by user interaction: // by https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload#Example window. addEventListener("beforeunload", function(event) { event. preventDefault(); // Cancel the event as stated by the standard.

How do you call a window Onbeforeunload?

The short answer is you can't do this. The onbeforeunload event can only be used to trigger that popup, it can't be used for anything else. Your only options for something like this are to attach an event to every outbound link on your page.


1 Answers

.onbeforeunload() is global. You can't have it trigger for only some ways of leaving the page. If the event handler is installed it will trigger no matter how the viewer is leaving the page.

You can keep track of your own internal state and decide whether to prompt anything in the onbeforeunload() handler or just return nothing (so no prompt is made).

So, you could have code like this:

//this identifies the unique page name
var viewName = $('input[name="SWEView"]').val(); 
// if I am on desired page bind the event so that now dialog is presented to user
if(viewName == "XRX eCustomer Create Service Request View"){
    $(window).bind('beforeunload', function() {
        // figure out whether you need to prompt or not
        if (myStateSaysToPrompt) {
            return 'Your Inquiry has not been submitted yet.';
        } else {
            return;   // no prompt
        } 
   });
}

Update

As this is quite high on google getting many views it would be worth noting that this answer no longer works.

 $(window).bind('beforeunload', function() {

Note that there is no longer 'on' in the event being bound. Using the previous onbeforeunload in later versions of jQuery will result in nothing happening.

like image 180
jfriend00 Avatar answered Oct 18 '22 06:10

jfriend00