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?
If you have HTML like this:
You would do something like this with jQuery:
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 thereturn false;) and prevents the browser from going to the page specified. If they have Javascript disabled, however, it would take them tono_javascript.htmldirectly.