SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast Mark_Elford's Avatar
    Join Date
    Oct 2003
    Location
    England, Wiltshire
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Conditional BeforeUnload

    ?Hi is there anyway to make a before unload event dynamic based on a flag..

    Essentially, I would like the user to be prompted that application has not been completed on back, close etc. I however do not want the user to be prompted if they have completed the application.

    I have a function on submit where I plan to set the flag to true.


    I would expect something like the following but it doesnt work for me?


    function unloadMess()
    {
    mess = "Leaving this page without submitting the application form will clear DOB entries. Do you wish to leave the application form?"
    return mess;
    }
    function setBunload(on)
    {
    window.onbeforeunload = (on) ? unloadMess : true ;
    }

    if (flag = "N") // Application form has not been submitted.
    {
    setBunload(true);
    }
    else
    {
    setBunload(false);
    }

    Any Ideas?

    Thanks,.

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think you'll find onbeforeunload is IE only. I was looking to use it the other day.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    SitePoint Enthusiast Mark_Elford's Avatar
    Join Date
    Oct 2003
    Location
    England, Wiltshire
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you may be right, But I am using ie. and I can set onbefore unload static by doing the following.. but cant get the condition in.

    function unloadMess()
    {
    mess = "Leaving this page without submitting the application form will clear DOB entries. Do you wish to leave the application form?"
    return mess;
    }
    function setBunload(on)
    {
    window.onbeforeunload = (on) ? unloadMess : true ;
    }

    setBunload(true)

  4. #4
    ********* Janitor levik's Avatar
    Join Date
    Jan 2003
    Location
    New York
    Posts
    885
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not check the flag in the function you call before the unload? For example:

    Code:
    function checkCompletion() {
        if (!completed) {
              return confirm('You haven\'t finished. Are you sure you want to leave?');
        } else { 
              return true;
        }
    }

  5. #5
    SitePoint Enthusiast Mark_Elford's Avatar
    Join Date
    Oct 2003
    Location
    England, Wiltshire
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically, I am writting a web application. The script runs fine up untill I reach the application form. What I want is to ensure that the user knows that clicking the back button will loose his information.

    Setting flags would work great if the user was to validate what they have entered. However, as I said they could just hit the back button. So no function is called and hence no pop up to warn them. The reason I want a toggled flag is if the application form is completed I do not want the user to get the warning that the application form is completed when it is.. when they unload the form and carry on with the entry.

  6. #6
    ********* Janitor levik's Avatar
    Join Date
    Jan 2003
    Location
    New York
    Posts
    885
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But the code I provided would do just that...

    Yuo initialize the variable completed to be false - then set it to true once the form is fully filled out (I imagine you can do this via some onChange handlers)

    Then set the function as the handler for the before unload event.

  7. #7
    SitePoint Enthusiast Mark_Elford's Avatar
    Join Date
    Oct 2003
    Location
    England, Wiltshire
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But wont the before unload event prompt you to complete the survey and then exit the page, Not give you the option to stay on the page.?

  8. #8
    ********* Janitor levik's Avatar
    Join Date
    Jan 2003
    Location
    New York
    Posts
    885
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought that returning false in the function assigned to the event handler would prevent that...?

    Honestly, I am not familiar with that particular handler (onbeforeunload) but if it's like all the others, then returning false would cancel whatever action it was about to signify. (For example returning false in onClick of a link would stop the user from navigating to the link's URL)


Bookmarks

Posting Permissions

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