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
https://forums.phpfreaks.com/topic/88484-solved-onunload-help/
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
https://forums.phpfreaks.com/topic/88484-solved-onunload-help/#findComment-452985
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
https://forums.phpfreaks.com/topic/88484-solved-onunload-help/#findComment-453167
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
https://forums.phpfreaks.com/topic/88484-solved-onunload-help/#findComment-453463
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.