Forum Moderators: phranque

Message Too Old, No Replies

JavaScript Prompt redirect to new page?

How do I do this?

         

RobbieD

2:20 am on Oct 5, 2003 (gmt 0)

10+ Year Member



How would I have someone click a link with a JavaPrompt Message "Would you like to continue?" If they click Ok it takes them to the URL if they click Cancel then it does nothing..just closes the prompt?

Something like this but this will not work...

<a href="http://www.thiswebpage.com" javascript:confirm('Do you want to continue to this webpage?');">This Webpage</a>

Thank you.

macrost

2:31 am on Oct 5, 2003 (gmt 0)

10+ Year Member



Try this!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function gosomewhere(){
var yes = confirm("Would you like to go to this webpage?");
location.replace("http://www.msn.com");
}
//-->
</SCRIPT>
</head>

<body>
<a href="#" onclick="javascript:gosomewhere();">Click Me</a>

</body>
</html>

RobbieD

2:34 am on Oct 5, 2003 (gmt 0)

10+ Year Member



Ok thank you!

Is there a way to do it without putting code between the head tags?

Thanks again!

RobbieD

2:43 am on Oct 5, 2003 (gmt 0)

10+ Year Member



Tried it and it works good except when you click Cancel it takes you to the URL. It should just close the box. How can you code it so when the user clicks Cancel it just closes?

RobbieD

2:52 am on Oct 5, 2003 (gmt 0)

10+ Year Member



Anyone? Everything is working with the prompt the Ok takes you to the URL but the cancel is also taking you to the same URL. I want it to just close the box when the user clicks "cancel".

RobbieD

3:20 am on Oct 5, 2003 (gmt 0)

10+ Year Member



Just need to get this one thing fixed and I'll stop posting ;)

The code at the top that macrost posted is working just not the Cancel button. It also takes you to the URL and should just close the prompt.

MonkeeSage

3:32 am on Oct 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RobbieD:

You can do it all inline like:

<a href="http://www.thiswebpage.com" onclick="if (confirm('Do you want to continue to this webpage?')) { window.location.href = this.href } return false;">This Webpage</a>

But it would be alot cleaner (and much easier if you have more than one link you want to use the effect on) to use a function, like...

<script type="text/javascript">
<!--
function navConfirm(loc) {
if (confirm('Do you want to continue to this webpage?')) {
window.location.href = loc;
}
return false; // cancel the click event always
}
//-->
</script>

Then make your links like:

<a href="http://www.thiswebpage.com" onclick="return navConfirm(this.href);">This Webpage</a>

Jordan

macrost

3:38 am on Oct 5, 2003 (gmt 0)

10+ Year Member



Jordan,
Thanks, I totally forgot about cancelling the function. ;)

RobbieD,
It would be better to have it in the head section, especially if you have more than one link, and it's a lot easier to debug.

Mac

RobbieD

3:43 am on Oct 5, 2003 (gmt 0)

10+ Year Member



Great thanx! It works great!

MonkeeSage

5:24 am on Oct 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RobbieD:

You can make the confirm prompt dynamically display the link address:

...
if (confirm("Do you want to continue to: \n" + loc)) {
...

Jordan