Is there a better way to "fake" the href attribute for links?

Now I am using:

href="javascript:void(0)" 

for my tags.

However, I find this messy, and it also appears to the user in FireFox on hover.

What I'm looking at is replacing my links with simple tags and setting up event handlers in JavaScript.

Clarification:

This is only for modern browsers with JavaScript support. I'm not interested in availability yet.

+4
source share
4 answers

Not sure what you are trying to do, but you can also use href="#" and use unobtrusive javascript to stop the click event from continuing.

+2
source

HTML

 <a href="">Foo</a> 

JS :

 var a = document.getElementsByTagName('a'); for(i=0 ; i<a.length ; i++){ a[i].addEventListener('click', function(e) { if (this.href === window.location.href) { e.preventDefault(); } }); } 

Demo : http://jsfiddle.net/pqNfg/

+2
source

use

event.preventDefault() , to disable any links that may be triggered by the user, clicks

 anchorElement.onclick=function(e) { e.preventDefault();} 
+1
source

I use too

  href="javascript:void(0)" 

Another way -

 href="same_path_where_link_is_clicked" 
0
source

All Articles