...

View Full Version : JS alert box with new window



maddogg
01-25-2006, 07:10 PM
Hello,
I'm trying to create an alert box from an image button that says something like "You are now leaving...do you want to continue?" I'd like an OK and a Cancel button and if the user clicks OK, it opens a new browser with the URL that the button was going to. And if the user cancels, it just closes the alert box.
Any ideas how to accomplish this?

konithomimo
01-25-2006, 07:17 PM
use a confirm box:


<script type="text/javascript">
function confirm_leave()
{
input_box=confirm("Click OK to continue or Cancel to stay here");
if (input_box==true)
{
// Output when OK is clicked
}

else
{
// Output when Cancel is clicked
}
}
</script>

maddogg
01-25-2006, 07:21 PM
Thank you konithomimo.

what about the script on the button itself?

mentalhorse
01-25-2006, 07:24 PM
Put this in any element that fires a onClick event.
onClick="confirm_leave()">Whatever

maddogg
01-25-2006, 07:30 PM
Works like a charm! Thank you konithomimo and mentalhorse for your help!

maddogg
01-25-2006, 07:34 PM
Oops, one more thing. When I hit Cancel, it still opens a new browser. Is there code that I can place in the Else statement that tells it to do nothing?

konithomimo
01-25-2006, 07:38 PM
It depends on how your code is set up. Post it here and we will see.

maddogg
01-25-2006, 07:42 PM
Here's the total code:

<head>

<script type="text/javascript">
function confirm_leave()
{
input_box=confirm("Click OK to continue or Cancel to stay here");
if (input_box==true)
{
// Output when OK is clicked
}
else
{
// Output when Cancel is clicked
}
}
</script>

</head>

<body>
<a href="http://www.amazon.com" target="_blank" onClick="confirm_leave()"><img src="image/photo1.jpg" width="50" height="10" border="0"></a>
</body>

konithomimo
01-25-2006, 08:32 PM
the following:

<a href="http://www.amazon.com" target="_blank"

should be:

<a href="#"

and then your script should be:


<script type="text/javascript">
function confirm_leave()
{
input_box=confirm("Click OK to continue or Cancel to stay here");
if (input_box==true)
{
window.location.href = "http://www.amazon.com";
}
else
{
return false;
}
}
</script>

maddogg
01-25-2006, 09:35 PM
You would think that should do the trick but I'm getting different issues now. The Ok button opens the new browser window for Amazon.com underneath the page already opened. And the Cancel button still opens another browser window. This new window is the same as the window already opened.

Here's the total code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

<script type="text/javascript">
function confirm_leave()
{
input_box=confirm("Click OK to continue or Cancel to stay here");
if (input_box==true)
{
window.location.href = "http://www.amazon.com";
}
else
{
return false;
}
}
</script>

</head>

<body>
<a href="#" target="_blank" onClick="confirm_leave()"><img src="images/photo1.jpg" border="0"></a>
</body>
</html>

konithomimo
01-25-2006, 10:02 PM
Remove:

target="_blank"

Also, you can change the window.location.href line to open up a popup. that should fix everything.

maddogg
01-25-2006, 10:13 PM
Cool, that seemed to do the trick. I replaced the window.location.href with popup = window.open("http://www.amazon.com", 'popup') and it looks like it's working. Thanks konithomimo for all your help! That was very nice of you.

konithomimo
01-25-2006, 10:25 PM
no problem. If you need any more help, feel free to ask. I spend more time at the JS section of WebDevelopers (http://www.webdeveloper.com/forum/forumdisplay.php?f=3) though.

glenngv
01-26-2006, 10:07 AM
This is the better solution as the link is still navigable even if javascript is disabled.

<a href="http://www.amazon.com" target="_blank" onClick="return confirm('Are you sure?');"><img src="image/photo1.jpg" width="50" height="10" border="0"></a>

And the code is even shorter and simpler.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum