Greg Burman

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromGreg Burman
ToMe
SubjectQuestion about Unload Event handling
Date24 August 2005 22:13
Greetings Mark,

First thanks for a really great JavaScript /DHTML resource! 'HowToCreate'
has become a standard point of reference when we are
looking for JS answers.

Although our question is not really involving any of your scripts, we are
hoping you can help as we are just plain stuck. As you
will see, this is not a quick one-liner so we'll definitely understand if
it's a bit much.

A little background is perhaps helpful:

We have several forms where we need to force a
	form.submit()
even if the user abandons the form without explicitly submitting it.

The purpose is to see what fields are causing the users to give up. Note -
we have a strict privacy policy and we do not keep any of
the submitted data when forms are abandoned. We are only interested in what
/fields/ they did and did not complete, with the idea of
then making the form better.

Our approach is to keep track of field entry/exit using onFocus() and
onBlur() and then force a form submit when the Unload() event
is fired. Then our server-side code sorts out what happened to what fields.

Quite often our forms will be opened in either a separate window or in an
IFrame.

Our first problem:

If the user closes the browser window altogether, it seems that unless we
issue an alert() of some kind /after/ we trigger the
submit(), the submit() never takes place. The alert() seems to keep the
window open long enough for the submit() to actually happen.

Unfortunately that leaves us with the second problem:

the Unload() event fires under all kinds of circumstances,  not just when
the user is actually closing the browser, (e.g. - going
'back', clicking outside the frame the form is in, etc., etc.) so we end up
having alerts pop up at times when it is not really
appropriate, when the /only/ time we really need it is if the browser window
is being physically closed.

So far we have the script below, where we trap the event.clientX for IE to
see if the user physically clicked outside of the form
area (ie to see if they clicked chrome somewhere), and that seems to work
reasonably well however outside of IE the alerts are often
quite incongruous.

We just can't seem to find a reliable way to tell how/why the unload event
is being fired so that we can adjust the alert message.

So, two questions really:

Any ideas on how we can detect the window being physically closed without
issuing the alert() ?

If not, any ideas on how to detect the various /other/ actions that fire the
Unload event, so that we can deduce whether the window
is being closed by process of elimination?

Right now we have the code below, which helps with IE, as we can tell if the
user clicked outside of the document itself, but even
that does not seem all that reliable and it leaves Opera and Gecko behaving
rather badly.

Any suggestions?

Any help comments greatly appreciated!

Greg

[sample code]
FromMe
ToGreg Burman
SubjectRe: Question about Unload Event handling
Date25 August 2005 08:11
Greg,

> We have several forms where we need to force a form.submit() even if
> the user abandons the form without explicitly submitting it.

Personally I would be tempted to try a faster method, such as building
up the data yourself, and using XMLHttpRequest or (new Image()).src= to
send the data back to the server. However, you may be stuck with having to
use an alert as well, since the browser will usually close the window too
fast.

> the Unload() event fires under all kinds of circumstances,  not just
> when the user is actually closing the browser, (e.g. - going 'back',

yes, that is intentional (same with reloading)

> clicking outside the frame the form is in

that sounds like a bug ...

> the /only/ time we really need it is if the browser window is being
> physically closed.

Then I suggest not relying on the individual onunload events for the frames.
Instead, use onunload for the parent frameset element. That should only fire
when the entire frameset is unloaded (when they close the window, or when
they reload the entire frameset, or if they go to a different website
replacing yours).

This technique should work in all browsers, not just IE.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromGreg Burman
ToMe
SubjectRe: Question about Unload Event handling
Date26 August 2005 16:34
Thanks Mark, good thoughts, we'll see what transpires

Cheers

Greg
FromGreg Burman
ToMe
SubjectDragable Div in a Form
Date6 April 2007 08:41
Hi!

Thanks for your scripts they have made life much easier on many occasions!

I have used the generic dragable Div/Layer script a number of times, but in
this case I have hit a wall and am hoping you might have
an idea....

I have a series of divs that are dragable /and/ contain form fields.

The problem is (or appears to be) that when the user goes to click on a
field in the form (inside the dragable layer) , the mouse
events are trapped by the Div's settings 	
(onmouseover="this.onmousedown=dragIsDown;" ondragstart="return false;"
onselectstart="return false;")

so they can't actually enter any data.

I tried re-setting the event traps in form field itself like this:

 <div class="settingsDiv" id="formSettingsDiv"
onmouseover="this.onmousedown=dragIsDown;"
ondragstart="return false;" onselectstart="return false;">

 <input type="text" id="formTitle" name="formTitle" onmouseover=""
ondragstart="" onselectstart="">

but no luck - I can't figure out how to get the event traps to be ignored
inside the form fields...

It works in I.E. 6.0 and /not/ in Firefox (1.0.4)

Also, even in Firefox the user (me) /can/ select from a drop-down menu in
the div but can't type into a text box, so maybe its not
the event traps at all...

Any suggestions?

Thanks again!

Greg
FromMe
ToGreg Burman
SubjectRe: Dragable Div in a Form
Date6 April 2005 22:03
Greg,

> when the user goes to click on a field in the form

... the mousedown event is trapped by the script, and is cancelled. Firefox
chooses to disable the resulting form focus as well. You need to make the
form input detect mousedown, and prevent the mousedown event from bubbling
up to the dragable div.

function stopit(e) {
    if( !e ) { e = window.event; } //non-standard (IE)
    if( e.stopPropagation ) { e.stopPropagation(); } //standards
    e.cancelBubble = true; //IE compatible
}
...
<input type="text" id="formTitle" name="formTitle"
onmousedown="stopit(arguments[0]);">
FromGreg Burman
ToMe
SubjectDragable Div in a Form
Date7 April 2007 02:22
Thanks v much!

Greg
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.