Jump to content

[SOLVED] onUnload Help


ballhogjoni

Recommended Posts

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()">

Link to comment
Share on other sites

 

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>

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.