26

How can I stop an anchor tag redirecting to another page if the user is not logged in? I have a function to check if the user is logged in or not. I want to do it with JQuery or JavaScript. Here is what I have tried so far but with no results:

<a href="www.somewhere.com" id="yes">Read More</a>

<script type="text/javascript">
    $(document).ready(function(){
        $('a').click(function(){
            var id = $(this).attr('id');
            if(id == 'yes'){
                //i want to prevent
            }else{
                //redirect
            }
        });
    });
</script>
9
  • I would maybe suggest you use sessions that this method Commented Jun 28, 2012 at 8:38
  • You can return false, when an onclick event is triggered.
    – funerr
    Commented Jun 28, 2012 at 8:38
  • Authentication needs to be performed server-side in your PHP, not in JavaScript at all.
    – Greg
    Commented Jun 28, 2012 at 8:38
  • You have to return false
    – Nhu Trinh
    Commented Jun 28, 2012 at 8:38
  • 1
    I expect you have a typo in your code, you probably don't mean to have $id but rather id as the variable name in the first few lines
    – Billy Moon
    Commented Jun 28, 2012 at 8:45

4 Answers 4

61

Please use http://api.jquery.com/event.preventDefault/

demo http://jsfiddle.net/aRBY4/6/

e.preventDefault() 

quote

If this method is called, the default action of the event will not be triggered.

Also if I may suggest read this: .prop() vs .attr()

Hope this helps,

sample code

  $('a').click(function(event){
    event.preventDefault();
    //do whatever
  });

In your case please try this

$(document).ready(function() {
    $('a').click(function(event) {
        var id = $(this).attr('id');
        if (id == 'yes') {
            event.preventDefault();
            //i want to prevent
        } else {
            //redirect
        }
    });
});​
7
  • i have used the same method you mentioned but it returns undefined on click if i alert id of anchor.
    – Ankit
    Commented Jun 28, 2012 at 8:42
  • @Ankit cool, so preventDefault aside, :) there is another issue with the selector which gets the id can you also paste your html in the question and I might help you out, cheers
    – Tats_innit
    Commented Jun 28, 2012 at 8:44
  • i was missing id as my page was showing same code for two cases , Thanks alot @Tats, problem resolved.
    – Ankit
    Commented Jun 28, 2012 at 8:49
  • one more Question. As i have prevented onclick of anchor tag i also want to stop open in new tab.
    – Ankit
    Commented Jun 28, 2012 at 10:20
  • Hiya @Ankit Stop opening tab: hmm either you can return false after preventDefault or use this API: api.jquery.com/stop worth thinking it through and asking a separate question :) just an idea if you cannot figure it out but above detail should help,
    – Tats_innit
    Commented Jun 28, 2012 at 10:23
4

Change your click event handler to this

$('a').click(function(e){
    var id = $(this).attr('id');
    if (id == 'yes')
    {
        e.preventDefault();
        e.stopPropagation();
    }
    else
    {
        //redirect
    }
});
0

try this one

    $("a").on("click",function(e){
         e.preventDefault();
         alert("Clicked");

     });

happy Coding;

-1

Use event.preventDefault(). It is used to prevent the default action of the event.

<script type="text/javascript">
$(document).ready(function(){
      $('a').click(function(){
          var id = $(this).attr('id');
         if(id == 'yes'){
             event.preventDefault() 
         }else{
            //redirect
         }
     });
});
</script>

Not the answer you're looking for? Browse other questions tagged or ask your own question.