Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Message Box to confirm link selection 3

Status
Not open for further replies.

briglass

Programmer
Dec 4, 2001
179
0
0
GB
Hello,

I have a hyperlink and when people click on the link, I would like a system (Windows, etc.) message box to pop up and confirm that they would like to continue.

A simple "OK" would be fine, although a "Yes" / "No" box would also be nice.

Is there a simple way to do this?

(This is because the website for my work contains links to copyright power-point presentations. I would like them to verify that they know that they cannot use these presentations without our permission.)



Thanks, (-:
Brian
 
Sure. You could redirect any requests for this kind of material to open via a javascript function. I have created a very simple solution below.

If the user clicks the OK button, it would let them through, if they pressed the CANCEL button, then it would not (there is no way I know of to change the button wording or position).

Specifically you can use window.confirm() (which returns true if they clicked YES, or false if they clicked CANCEL)...

Code:
<script type="text/javascript">
function openURL(_myURL)
{
  if (window.confirm("Do you agree to the copyright notice?") document.location = _myURL;
}
</script>

You would have to modify the <a href> to read like this for any documents you want the message to appear with:

Code:
<a href="javascript:openURL('copyrighted.doc')">View document</a>

Hope that gets you started,
Jeff
 
Jeff-

Thank you very much for your reply. My webpage has several different links pointing to different destinations, and so is there a way I could make this work for multiple destinations? It appears this would only work for a page with a single link for which I would like this message to appear.



Thanks, (-:
Brian
 
Sure... you apply the change in <a href> for each link that has that requirement. You can pass the full path to the document too. The following shows 3 links (all different) that could exist on the same HTML page together -- and still work fine with the alert:

Code:
<a href="openURL('copyrighted1.doc')">Doc 1</a>
<a href="openURL('/special/path/copyrighted2.doc')">Doc 2</a>
<a href="openURL('[URL unfurl="true"]http://www.xyz.com/documents/secret/copyrighted4.pdf')">Doc[/URL] 3</a>

Does that make sense? Of course you need the javascript on each HTML page you want to use this on.

Jeff
 
Jeff-

Yes, I understand where this code would go, however I'm still unsure about one item, please bear with me:

What do I replace with the _myURL in the java script, and does it need to be in quotes? Is it the URL of the current page?


Thanks, (-:
Brian
 
_myURL is a variable so it does not need to be in quotes. You pass the value of this variable when you call this function in the link. In this case, 'copyrighted1.doc' is the value being passed, and that does require the single quotes.

BabyJeffy, very nice example! I'm giving you a star!

- Zoe, that's ZOH-EEE, get it right please
- Just a little ol' MCP at Solien Technology
-
 
Thanks for all your help.

Hang in there with me...

I am receiving an Internal Script Error on the page. It says it is expecting a ')' at character 63.

Am I correct in saying that I do not need to change _myURL to anything when I implement the code? I just copy the script just as it is in your example?



Thanks, (-:
Brian
 
Briglass, you are correct - you dn't need to change _myURL to anything. As for your error, I see that there is a missing ")" in the IF statement. It should look like this:

if ( window.confirm("Do you agree to the copyright notice?") ) document.location = _myURL;

Note that this page wraps text automatically. This line of code should be all on one line to avoid confusion and errors.

- Zoe, that's ZOH-EEE, get it right please
- Just a little ol' MCP at Solien Technology
-
 
It worked! Thank you all very much!

Thanks, (-:
Brian
 
I need to get my act sorted on those brackets... this isn't the first time it's happened... thanks for helping out :)

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top