ballhogjoni Posted January 29, 2008 Share Posted January 29, 2008 I have the following script that shows a simple confirm dialog when a visitor leaves my page. I want this to just show up if the user doesn't submit the form on page. So if the user closes the window, types a new url in the address bar, or simply leaves my page, I want the confirm box to show up before they leave; I don't want the confirm box to show up if the user submits the form on my page. Can someone show me how to do this? <script language="javascript" type="text/javascript"> function exitWindow() { if (confirm("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")) { return false; } else { var newwindow = location.href = "http://xxxxxxxxxxxxxxxx"; if (window.focus) { newwindow.focus(); } } } </script> </head> <BODY bgcolor="#FFFFFF" onunload="exitWindow()"> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 try this: <script language="javascript"> var occured="no"; function ucantgoyet() { if (occured == "no") { confirm("Please Stay!"); document.location = document.URL; } } </script> </head> <body onunload="ucantgoyet()"> <form name="form1" action="somepage.php" method="post"> <input type="submit" value="Submit" onclick='occured="yes"; return true'> </form> Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted January 30, 2008 Author Share Posted January 30, 2008 THANK you foryour help. The problem is when I submit the form, it doesmn't change the variable to yes, therefore it still asks the confirm question. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 when I submit the form it does; don't submit the page back to itself or you will keep getting the confirm prompt. if your going to submit the page back to itself; you need to dynamically change the "occured" variable to "yes" or something else completely. do something like this: <script language="javascript"> var occured="<?php if (isset($_POST['submission'])) { echo "yes"; } else { echo "no"; } ?>"; function ucantgoyet() { if (occured == "no") { confirm("Please Stay!"); document.location = document.URL; } } </script> </head> <body onunload="ucantgoyet()"> <form name="form1" action="somepage.php" method="post"> <input type="submit" name="submission" value="Submit" onclick='occured="yes"; return true'> </form> Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted January 30, 2008 Author Share Posted January 30, 2008 I'm not submitting the page to itself, this is code I have is: <script language="javascript"> var occured="no"; function ucantgoyet() { if (occured == "no") { if (confirm("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")) { } else { document.location.href = "https://xxxxxxxxxxxxx"; } } } </script> </head> <BODY bgcolor="#FFFFFF" onunload="ucantgoyet()"> <script type="text/javascript">document.write("<input type=\"button\" name=\"Submit\" value=\"CLAIM YOUR KIT\" onClick=\"check(form,form.elements.length)\" onClick=\"occured='yes';\">");</script> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 Your using two onclicks for one button; not two onclick events; but two actual onclicks. That might not be your only problem, but it's diffidently one you need to fix. You need to combine the two onclick events into one onclick. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.