Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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...

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I am trying to call if from a window.unbeforeunload... what do you mean with a "synchronous HTTP request"?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •