JavaScript: submitting form on window close

Patt

Diamond Member
Jan 30, 2000
5,288
2
81
OK, I've got a client who wants form data submitted no matter how the user navigates away from his form, even if he clicks the (X) to actually close the browser.

I've been playing around with the beforeonunload function and onunload function, but I can't seem to get the form submitted before the Window actually closes.

For the record, I don't agree with him doing it this way, and I'm even thinking of just trying to pop up a confirm dialog that the user wants to leave the form instead. If anyone has any ideas, I'd really appreciate some help!
 

Pacemaker

Golden Member
Jul 13, 2001
1,184
2
0
setting an onunload event in the body tag sounds like your best bet, but I have no idea if that is triggered if the (X) is clicked.

Edit: apparently it does I just checked.
 

Patt

Diamond Member
Jan 30, 2000
5,288
2
81
If I do that, I guess I also have to throw in a 'dirty flag' type of checking to see if the form needs to be submitted.

Unfortunately though, the onunload gets called on more than just the (X) click.
 

Pacemaker

Golden Member
Jul 13, 2001
1,184
2
0
Yeah, there isn't a really good way to do it. The request is not reasonable (I know it's probably not your call). I mean how difficult is it to press a button when you are done? Isn't this just basic website usage?
 

Patt

Diamond Member
Jan 30, 2000
5,288
2
81
Oh, believe me, I keep trying, and he just keeps asking the same damn question if he doesn't like my answer :p
 

nakedfrog

No Lifer
Apr 3, 2001
63,691
20,146
136
Add a div that encompasses the entire page which sets a flag onmouseout, and only fire the onunload confirmation if that flag is set? Might work.
 

Patt

Diamond Member
Jan 30, 2000
5,288
2
81
Originally posted by: nakedfrog
Add a div that encompasses the entire page which sets a flag onmouseout, and only fire the onunload confirmation if that flag is set? Might work.

Interesting idea ... I'm going to see if I can make that work.
 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
If you're going to do that, do it on blur so people can use the keyboard.
 

Patt

Diamond Member
Jan 30, 2000
5,288
2
81
It almost works ... so close to what he wants (minus the validation code which he can damn well write himself). There is something that is setting the flag to 'out' for some reason, and once I figure that out, he'll get the code, and I'll close the call at last!

Thanks everyone for the help.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
How does he intend to prevent people like me from using it? I only enable javascript on pages where I deem it improves my experience.
 

nakedfrog

No Lifer
Apr 3, 2001
63,691
20,146
136
Originally posted by: sourceninja
How does he intend to prevent people like me from using it? I only enable javascript on pages where I deem it improves my experience.

80/20 rule, most likely.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
To be honest I think this is close to deceptive (I know it's not your idea, OP). I don't like the sound of it much. In any case, you'll have to settle for almost as there will always be ways to bail out of the browser without submitting the data, and in fact if the submit happens on a nav event it may be flagged as suspicious, either now or in the future.
 

nakedfrog

No Lifer
Apr 3, 2001
63,691
20,146
136
Originally posted by: Markbnj
To be honest I think this is close to deceptive (I know it's not your idea, OP). I don't like the sound of it much. In any case, you'll have to settle for almost as there will always be ways to bail out of the browser without submitting the data, and in fact if the submit happens on a nav event it may be flagged as suspicious, either now or in the future.

Yeah, I've been in that boat... clients ask for some icky stuff sometimes.
 

QuixoticOne

Golden Member
Nov 4, 2005
1,855
0
0
That's darn near spyware in the sense that no information should be sent to the server without the intention of the user that it be sent. If I don't click {SUBMIT} but I instead {X} the browser, that doesn't represent my intention to send any further data to the web site.
In fact it is a pretty sure sign of me emphatically "opting out" of any further interaction with the site.
It's probably questionably illegal, barring any disturbing questions of TOS/EULA/Privacy policy etc.

But just because a malware site may have a TOS that says "oh yeah, by loading this site {which you've already done, so it's too late to opt out} you give us permission to upload anything / everything from your PC to our server even without further consent / knowledge on your part, have a nice day, thanks for your banking information and passwords." doesn't make that ethical / legal / whatever.

Slippery slope...

 

QuixoticOne

Golden Member
Nov 4, 2005
1,855
0
0
And like sourceninja said, this sort of thing is a great reason why I deny javascript / active x / java / flash / .... active content permissions to *every* site with the selective exception of a very few. If a given site doesn't work without active scripting, generally, they lose my patronage and my trust / respect.
 

Patt

Diamond Member
Jan 30, 2000
5,288
2
81
I actually convinced the client that he doesn't want to do this. Now he has to sell it to his superiors :p I'm in the same boat where I don't like the idea that anything would be submitted on [X] ... contravenes normal conventions. About the only legitimate use I could see was logging someone's session server off if they didn't do it explicitly.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: Patt
I actually convinced the client that he doesn't want to do this. Now he has to sell it to his superiors :p I'm in the same boat where I don't like the idea that anything would be submitted on [X] ... contravenes normal conventions. About the only legitimate use I could see was logging someone's session server off if they didn't do it explicitly.

Yeah, that keeps popping up. Problem is it doesn't buy you anything. If you can't completely rely on the session getting closed (and you can't because the user can terminate the process) then you have to code all the same safeguards anyway.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
I didn't even think of it from a spyware point of view. I would concider that spyware, just like having a cancel button that actually submits the data anyways.
 

Patt

Diamond Member
Jan 30, 2000
5,288
2
81
Update: I was successful ... they will NOT be applying any logic to the close, and although interesting, none of the above arguments need to be considered anymore. Thank you everyone for the input!
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
hooray for doing the right thing!

btw, you can also do this with some ajax that stores the data typed in as its being typed in (for the record)