Question:

How can I make an alert appear under certain circumstances when clicking a link, and then not go to the link?

by  |  earlier

0 LIKES UnLike

I am going to have a hidden value on my web page. If it contains a certain value (5 in this case), when a link is clicked, rather than executing the link, I would like an alert to display. Is there a way to do this?

I was thinking of having a JavaScript function something like this:

function validateLink()

{

if (document.myform.myfield.value == "5")

{

alert("You cannot visit this page");

}

}

Then doing something like this

<a href="www.mypage.com" onclick =validateLink()">My link </a>

Is there a way if the alert in validate link is displayed to not direct the user to www.mypage.com?

Thank you so much!

 Tags:

   Report

3 ANSWERS


  1. Just a comment...

    Anyone savvy enough is surely going to check the source code and go there directly, aren&#039;t they?  This means protection on the actual destination page itself would be needed, won&#039;t it?

    EDIT:

    No, in reply to first poster; it isn&#039;t useless.  You may want to restrict access to your site to family and friends, for example, or direct people to a particular page based on a result.  The poster wants the method for that kind of restriction/action.


  2. You have the right idea here but its not going to work very well with JavaScript because, like the previous answer says, someone can just look at your JavaScript. Try using PHP to do this instead...its far more secure. Theres a nice tutorial on it here:

    http://www.w3schools.com/php/default.asp

    And yes its a perfectly useful thing to do!


  3. Why?  Pretty useless to me.

    Ron

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.