57

I'm trying to load some stuff using AJAX when a user clicks a link, but I want the link to actually go somewhere so that the app still works when javascript is disabled. Is there any way to just do something with javascript and cancel navigation when a link is clicked?

What's the best practice? Can that be done, or do I need to replace the link using javascript or what?

John
  • 1
  • 13
  • 98
  • 177
Max Schmeling
  • 12,363
  • 14
  • 66
  • 109

10 Answers10

55

If you have HTML like this:

<a href="no_javascript.html" id="mylink">Do Something</a>

You would do something like this with jQuery:

$(document).ready(function() {
    $('#mylink').click(function() {
        doSomethingCool();
        return false; // cancel the event
    });
});

All you have to do is cancel the click event with Javascript to prevent the default action from happening. So when someone clicks the link with Javascript enabled, doSomethingCool(); is called and the click event is cancelled (thus the return false;) and prevents the browser from going to the page specified. If they have Javascript disabled, however, it would take them to no_javascript.html directly.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 3
    +1, but a lot of people will put href="#" in the links they plan on using with javascript. – Gromer May 15 '09 at 01:01
  • okay, I'm an idiot. I thought that was the thing to do but it wasn't working. turns out it was just IE caching the javascript so it wasn't executing my new line with return false. – Max Schmeling May 15 '09 at 01:03
  • 1
    @Gromer - The idea is to make it work with or without javascript. – Max Schmeling May 15 '09 at 01:03
  • @Gromer - But, the point of the question that Max asked was if there was a way for the link to work as usual if the user doesn't have javascript enabled. Of course, href='#' would work, but it wouldn't do anything (other than change the URL hash), and hence lead to a poor user experience. – KyleFarris May 15 '09 at 01:08
  • 1
    No as my code is, David. Only if you neglect to return false; – Paolo Bergantino Jul 10 '09 at 18:12
  • Try this href: "javascript:void(0)" – Israel Saraiva Mar 25 '21 at 13:16
47

None of this worked for me with the Google Chrome browser.

Here's what did work though (using jQuery):

$(document).ready(function() {
    $('#mylink').click(function(event) {
        doSomethingCool();
        event.preventDefault(); // cancel the event
    });
});
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
Partap Davis
  • 639
  • 6
  • 3
31

why jQuery?

HTML:

<a href="no_javascript.html" onclick="return doSmth()">Link</a>

...and javascript code:

function doSmth(){
  // your code here
  return false
}
Sergei Kovalenko
  • 1,402
  • 12
  • 17
  • Well I am working with jQuery (like the majority of people on this site it seems), even though I didn't specify... but thank you for the post, it may be useful to somebody. – Max Schmeling May 15 '09 at 14:09
  • 1
    ok, i see. I think frameworks may be useful to big powerful projects. But for small sites... – Sergei Kovalenko May 17 '09 at 17:25
  • 4
    Javascript frameworks are useful on all size sites that care about browser compatibility. Plus, using inline events is a bad practice overall. – Paolo Bergantino Jul 10 '09 at 18:14
  • there are no needed frameworks at http://4px.ru/ (: Using inline event isn't bad for one element. And for many similar elements with similar actions we can set event handlers by script. – Sergei Kovalenko Jul 12 '09 at 09:08
  • 3
    THANKYOU!!! man... people let go of jQuery ... at least if you do not need its more higher functions! Thanks for the post! -- This also works onclick = "return false;" This is handy if you are using google polymer and using the on-tap="{{func}}" handyness – Wes Duff Jun 18 '15 at 01:08
  • 3
    +1 for not laming out like an amateur and dumping 70KB of crap that the browser *already* competently handles. – John Dec 20 '17 at 18:13
9

What I would do are :

a. for href attribute, javascript:void(); will be set

b. for onclick event, will provide a function to handle the click event and that function will return false.

eg:

<script type="text/javascript">
   function myfunction()
   {
      //do something
      return false;
   }
   </script>

   <a href="javascript:void();" onclick="myfunction()">Link</a>
taco
  • 1,367
  • 17
  • 32
jsa
  • 369
  • 2
  • 7
4
<a href="http://www.google.com" class="ignore-click">Test</a>

with jQuery:

<script>
    $(".ignore-click").click(function(){
        return false;
    })
</script>

with JavaScript

<script>
        for (var i = 0; i < document.getElementsByClassName("ignore-click").length; i++) {
            document.getElementsByClassName("ignore-click")[i].addEventListener('click', function (event) {
                event.preventDefault();
                return false;
            });
        }
</script>

You assign class .ignore-click to as many elements you like and clicks on those elements will be ignored

Andrew
  • 7,619
  • 13
  • 63
  • 117
3

for IE at least, just put this.event.returnValue = false; at the end of the method.

e.g:

function onClickFunction()
 {
  someMethod();
  this.event.returnValue = false;
 }

I had a complex method where this did the trick.

mbinette
  • 5,094
  • 3
  • 24
  • 32
chill
  • 31
  • 1
1

I use document.location.replace to navigate, but when I want to cancel the navigation, return false (in the same function as document.location.replace) doesn't cancel the navigation.

DarthJDG
  • 16,511
  • 11
  • 49
  • 56
0

The best way should be this.

<a href="javascript:undefined"></a>
Kyad
  • 119
  • 3
  • 13
0

I just used

<a href="https://ryanhigdon.com" onClick={ event => {
  event.preventDefault()
}}>Take me away</a>

In my use case I wanted to perform some actions before navigation.

rhigdon
  • 1,483
  • 1
  • 15
  • 20
-6

is simple, use rel="nofollow" attribute

 <a href="signin.php" rel="nofollow">sign in</a>