Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > event bubbling

Reply
Thread Tools

event bubbling

 
 
Chris W
Guest
Posts: n/a
 
      07-30-2011
Hello everyone,

I've been trying to understand even bubbling, and not quite sure I
completely follow it. I started reading about it so that I could
warn users when they were leaving a page on my website if they had
started to enter data into a form.

I initially used the following used the following code based on [1]

var entereddata = false;

$(document).ready(function()
{
$('#contactform').bind('keypress',function(e) {
if((e.which > 96 && e.which < 123) || (e.which > 47 && e.which <
5) {
entereddata = true;
//alert(e.which);
}
});
});

function goodbye(d,e) {
if(!e) e = window.event;
if(!d) d = entereddata;

if(d)
{
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed
on the dialog

//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
}
window.onbeforeunload=goodbye;

but, this didn't work in Chrome. After a bit of googling and hacking
apart other code I found, I've found that this seems to work and is
cross-browser compatible:

var entereddata = false;

$(document).ready(function()
{
$('#contactform').bind('keypress',function(e) {
if((e.which > 96 && e.which < 123) || (e.which > 47 && e.which <
5) {
entereddata = true;
//alert(e.which);
}
});
});

function confirmLeave(e, d)
{
if(!e) e = window.event;//window.event;
if(!d) d = entereddata;

var confirmationMessage = 'It appears you have started to enter
information into the contact form, but have not yet submitted it';

if(!d)
{
e.cancelBubble = true;
} else
{
return confirmationMessage;
}
}

window.onbeforeunload=confirmLeave;

This works like I want. But, if enteredData is true, then I'm not
affecting the bubbling process. Should I be? Should I have
e.cancelBubble=false if enteredData is false and e.cancelBubble=true
if enteredData is true? What effect does setting the value of
e.cancelBubble actually have when closing a page?

Am I also correct in thinking I don't need the event e.stopPropagation
at all, because firefox supports event bubbling?

Thanks in advance,
Chris

[1] http://www.openjs.com/scripts/events...nfirmation.php
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      08-07-2011
On Jul 30, 10:25*pm, Chris W <(E-Mail Removed)> wrote:
> Hello everyone,
>
> I've been trying to understand even bubbling, and not quite sure I
> completely follow it. * I started reading about it so that I could
> warn users when they were leaving a page on my website if they had
> started to enter data into a form.
>
> I initially used the following used the following code based on [1]


By far the simplest method is to use the beforeunload event to check
if the value of any form control is different to its defaultValue, or
any radio buttons or checkboxes have been selected other than the
defaults.

The above strategy only requires a single listener and you don't need
to track user input.

--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Bubbling and the ItemCommand Event Nathan Sokalski ASP .Net Web Controls 0 05-29-2006 07:16 AM
Event Bubbling and the ItemCommand Event Nathan Sokalski ASP .Net 0 05-29-2006 07:16 AM
Event Bubbling and the ItemCommand Event Nathan Sokalski ASP .Net Datagrid Control 0 05-29-2006 07:16 AM
Event Bubbling from a template Barry ASP .Net 0 08-07-2003 04:54 PM
Re: Bubbling Event ---- Problem!!!! S. Justin Gengo ASP .Net 3 07-24-2003 10:11 AM



Advertisments