www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to make a Javascript Alert pop up onunload EXCEPT if they submit a form

  1. #1
    Join Date
    Nov 2009
    Posts
    1

    How to make a Javascript Alert pop up onunload EXCEPT if they submit a form

    Hi there

    I have a page with some forms on (the "action" page is a different page), the problem is users sometimes don't spot the forms and leave the page by other means.

    (it's a long story as to why they don't spot the forms and submit one of them, but the point is we need to remind them to submit one of the forms if they go to unload the page by any other way)

    I plan to use a javascript "alert" with the following message "Oops, you didn't click on one of the submit buttons, is that what you meant to do? If you want to go back and click on of the submit buttons click "cancel" or if you really wanted to leave this page then click "ok".

    So far so good.

    The problem is how to 'disable' the javascript alert in the event of them submitting one of the forms...


    My thoughts are

    Option 1: I could add some JS to the submit button such as 'onclick="disable the alert"'

    Option 2: I could have a hidden form variable in the page which defaults to zero.

    I then would make the function that launches the alert check the value of that variable and only continue if the variable = 0

    I would have each of the submit buttons do 'onclick="set the varible to = 1'

    - hope that makes sense.


    My knowledge of javascript is "beginner" so if anyone could help with how best to tackle this I'd be ever so grateful.

    TIA Nick

  2. #2
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    947
    HTML:
    Code:
    <body onunload="javascript:alertOrNot();">
    Form:
    Code:
    <form onsubmit="javascript:submitted = true;" //..... etc
    JS (head):
    Code:
    var submitted = false;
    function alertOrNot() {
    if (submitted == false) //then do your confirm prompt
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles