Reputation: 503
I just saw this question and it reminds me of something I usually do to prevent the default action of some link:
<a href="javascript://">something</a>
Then I handle it with jQuery click function. I already saw in some places people using
<a href="javascript:void(0)">something</a>
I know the better way is using
e.preventDefault();
But is it wrong or a bad practice to do what I do? How this actually works?
Upvotes: 1
Views: 194
Reputation: 490423
It is bad practice because a better mechanism exists and that is event.preventDefault()
.
Also, the javascript:
pseudo protocol should only be used for bookmarklets.
Upvotes: 3
Reputation: 112857
It's a bit unfortunate because if the user Ctrl+clicks or right clicks and says "Open in New Tab," they will get a completely blank page.
Using <a href="#">something</a>
plus preventDefault
is better, since in that case Ctrl+click just takes them back to the page they were on.
Of course, the very best is if you can have the href
point to a page that is actually meaningful, with the JavaScript being a progressive enhancement to the experience that overrides that meaningful default. A great example of this is popup windows, but with a bit more work anything can be made to do this.
Upvotes: 5