Can some please tell me how you can change the behaviour of a link <a href="somepage.htm</a> so that it opens a dialogue box with a message (which you can dictate) and lets you press ok before proceeding to the desired link page.

Please help. I'm guessing its an onclick event.

Many Thanks,


To get this to work, you need to put some code in both the <head> and the <body> of your webpage:

You will need to put this code into the <head> of your web page:

<script type="text/javascript">
function ConfirmChoice()
if (conf !="0") {

This part is placed where you want your link to appear.

<a href="#" onclick=" ConfirmChoice(); return false;">

If you need to use more than one link of this kind, it can be acheived by simply altering the function name.

Hope this helps! :)


<A HREF="#" onClick="javascript:alert('hi'); window.location='www.google.com'; return false;">

With her method, however, the user has a choice to hit cancel or not, while this is strictly an OK button.

I think these are a little off, though close.

First, the link you posted is wrong!

<a href="somepage.htm</a>

Should be:

<a href="somepage.htm">Link to somepage</a>

If you want a Javascript function to run when the user clicks the link, you code the "onclick" attribute:

<a href="somepage.htm" onclick="myFunc();">Link to somepage</a>

Now, here's the question. What do you want that script to do? I'm going to assume that you want the user to be able to "OK" or "Cancel" the navigation. In that case, the function will need to RETURN a true or false value. In order for the link to handle that, you need to add the "return" keyword to the link. Let's also pass in a message to display:

<a href="somepage.htm" onclick="return myFunc('Are you sure?');">Link to somepage</a>

Now for the function itself:

<script type="text/javascript">
function myFunc(msg)
  result = confirm(msg);
  return result;

Why does that work? Because the "confirm" dialog returns "true" if the user clicks "ok", and "false" if they click "cancel". That value, true or false, is stored in "result". The function "returns" the result value back to the link (actually, back to the event handling the link, minor detail).

If it receives "true", it follows the link. If it receives "false", it cancels the link.

In fact, the code within the function could be shortened to just:

<script type="text/javascript">
function myFunc(msg)
return = confirm(msg);

And if that's all you need to do, you don't need the function at all. Just do it all in the link:

<a href="somepage.htm" onclick="return confirm('Are you sure?');">Link to somepage</a>