Solved

Cancel navigation, close etc...

Posted on 2004-03-30
7
1,753 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.
0
Comment
Question by:bganoush
  • 3
  • 3
7 Comments
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 10714905
Did you try:

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

0
 
LVL 25

Accepted Solution

by:
devic earned 250 total points
ID: 10714988
you need here onbeforeunload event:
=============
<body  onbeforeunload="if(document.myForm.myCheck.checked) return 'You didn\'t saved your document' ;else return">
<form name='myForm'>
<input type=checkbox  name=myCheck>
<form>
0
 

Author Comment

by:bganoush
ID: 10724349
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
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:bganoush
ID: 10724765

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
0
 
LVL 25

Expert Comment

by:devic
ID: 10724950
hehe yes,  onbeforeunload, try example above:
0
 

Author Comment

by:bganoush
ID: 10725041
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
0
 
LVL 25

Expert Comment

by:devic
ID: 10725077
this is IE feature IE 4.0 +
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now