views:

1084

answers:

3

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?

+9  A: 

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
+1, but a lot of people will put href="#" in the links they plan on using with javascript.
Gromer
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
@Gromer - The idea is to make it work with or without javascript.
Max Schmeling
I didn't read that part :o Damn my skimming of questions!
Gromer
@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
href="#" does scroll the page back up to the top ...
David Dorward
No as my code is, David. Only if you neglect to return false;
Paolo Bergantino
how about canceling navigation of an iframe? say I make a post request within an iframe, and I want to cancel it sometime before completion. is it just enough to remove the iframe? (because I need to do so after canceling)
Morteza M.
+3  A: 

why jQuery?

HTML:

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

...and javascript code:

function doSmth(){
  // your code here
  return false
}
Sergey Kovalenko
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
ok, i see. I think frameworks may be useful to big powerful projects. But for small sites...
Sergey Kovalenko
Javascript frameworks are useful on all size sites that care about browser compatibility. Plus, using inline events is a bad practice overall.
Paolo Bergantino
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.
Sergey Kovalenko
A: 

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.