We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Cancel navigation, close etc...

bganoush
bganoush asked
on
Medium Priority
1,808 Views
Last Modified: 2007-12-19
I have a form that is rather long to fill in and I need a way to warn the user that the data has not been submitted before he/she leaves the page.

The main key for deciding if the form has been submitted is that a checkbox has been checked.

So far, I have a script that starts like this:

<script>
   function checkleave() {
    if (document.myForm.myCheck.checked) {
     alert ("Your data was not submitted, please click on the submit button before leaving.");
     // Help needed here...
     }
  }
</script>

And in my body:

<body onunload='checkleave()'>
<form name='myForm' ...

So what I would like to do is to return the user to my page after the alert...

Now it would be nice if the form was just submitted automatically and passthrough to where-ever the user wants to go without having to return to my page after the alert but that sounds hard and my brain is very tiny and kinda full right now so I'm not sure if I can handle that.
Comment
Watch Question

ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Zvonko,

Do you know if this works in all exit cases? The only ways out of a page seem to be clicking on a link, closing the window, a script redirection and typing a URL.... are there more ways?

-- Bubba

Author

Commented:

Actually, I tried the submit but the problem is that it will perform the action from the form but will not redirect to the user's original URL... For instance, if the user click a link that goes to MSN.COM but the FORM has and action that goes to YAHOO.COM, then the orignal link is ignored...

Ex: (Try this, it's pretty funny.)

<HTML>
<SCRIPT LANGUAGE='Javascript'>
function checkleave() {
  if (document.myForm.myCheck.checked)
    document.myForm.submit();
}
</SCRIPT>
<BODY onunload='checkleave();'>
<FORM name='myForm' action='http://www.yahoo.com'>
<INPUT type='checkbox' name='myCheck'> I prefer Yahoo
</FORM>
<A HREF='http://www.msn.com'> Go to MSN </A>
</BODY>
</HTML>

Note that when you click on "Go to MSN" with the checkbox turned off, then it goes to MSN, no accounting for taste... But if you click on the checkbox "I prefer Yahoo" then clicking on the "Go to MSN" link will go to Yahoo but will not continue on to MSN.

In my application, my form submits via a C++ CGI and I just want the form to be submitted to the CGI and then I want to redirect to the user's original click or URL whatever was requested.

PS: devic,
ONBEFOREUNLOAD????      Never heard of it... can I get one in paisley or to match my mom's Royal Daulton?

-- Bubba
CERTIFIED EXPERT

Commented:
hehe yes,  onbeforeunload, try example above:

Author

Commented:
Alright,

That does work... just one more thing...  Can I assume that onbeforeunload works on older versions of IE and Netscape?  My HTML manual doesn't mention it and it's only about 2 years old... (No it's not HTML for Dummies!)

-- Bubba
CERTIFIED EXPERT

Commented:
this is IE feature IE 4.0 +
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.