Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2013, 05:21 PM   PM User | #1
Fedy
New to the CF scene

 
Join Date: Jan 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Fedy is an unknown quantity at this point
How can I submit a form depending on some vars?

Hi, I'm trying to submit a form depending on the result of a var, and specifically it is a window confirmation to save the form if the user is leaving the page. This is the code I tried:

Code:
var savebeforeexit = window.confirm("Are you sure?"); 
		
if (savebeforeexit) {
			
   document.myform.submit();
			
} else {

   //do nothing
}
However this does not work...
Fedy is offline   Reply With Quote
Old 01-08-2013, 05:48 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,185
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And WHERE are you calling that code FROM?

If it is from window.onbeforeunload it will never work.

And quite frankly, window.onbeforeunload is the only reliable place to put a "now leaving the page" trap. But the most you can do there is allow the user to cancel his request to leave the page.

(I have wondered if you could make a synchronous HTTP request at that point. I think you could, but that's beginning to get pretty complex.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 01-10-2013, 08:11 AM   PM User | #3
Fedy
New to the CF scene

 
Join Date: Jan 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Fedy is an unknown quantity at this point
Yes, I am trying to call if from a window.unbeforeunload... what do you mean with a "synchronous HTTP request"?
Fedy is offline   Reply With Quote
Old 01-10-2013, 07:48 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,185
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Synchronous AJAX. Or better named SJAX, I suppose.

Code:
    var http = new XMLHttpRequest();
    http.open( "GET", "someURLonYourSite.php?xxx=3", false );
    http.send( );
The false there says "wait for an answer".
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 01-10-2013, 10:00 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,449
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Fedy View Post
Code:
var savebeforeexit = window.confirm("Are you sure?");
confirm() gives your visitor three alternative responses in most browsers - in some browsers the third alternative is to disable JavaScript completely for the web page at that point in which case the call would never return to assign a value to savebeforeexit.

Calls to alert() confirm() and prompt() should only ever be used for debugging, they should never be used in a live web page.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-10-2013, 11:04 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,185
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
All true, Felgall, but irrelevant. You can't use confirm() in onbeforeunload anyway.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 02:33 AM.


Advertisement
Log in to turn off these ads.