I occasionally see an HTML <a>
element whose href
attribute is a URI that has just javascript
for the scheme, and an empty statement ;
for the path.
What is the purpose of this?
Is it the same as href="javascript:void(0);"
?
Is it the same as having no href
attribute at all?
Is it the same as having no <a>
element at all?
Update
The precise content that I am seeing is <a href="javascript:;" style="cursor: default;"></a>
. So is this just a way of controlling the cursor graphic?
Definition and Usage The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!
JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.
Combining javascript: and void(0) Sometimes, you do not want a link to navigate to another page or reload a page. Using javascript: , you can run code that does not change the current page. This, used with void(0) means, do nothing - don't reload, don't navigate, do not run any code.
The href= “” will only load the current page, while href= “#” scrolls the current page to the top, while href= 'javascript:void(0)' will do nothing at all. The same effects of javascript:void(0) are realized by returning false from the click event handler of the <a> tag with either of the two methods.
You can understand by below some steps:
<a onclick="foo()">Next Image</a>
<a href="#" onclick="foo()">Next Image</a>
<a href="javascript:foo()">Next Image</a>
<a href="javascript:void(0)" onclick="foo()">Next Image</a>
<a href="#" onclick="foo(); return false;">Next Image</a>
method 1 usually won't change the mouse cursor to a "hand cursor", so maybe it is not as desirable in some cases.
method 2 seems to cause the page to shift to a new location sometimes on IE 6 and IE 7. (to top of page?)
method 3 ... should work... but is it an old school way and should be avoided for modern browsers?
method 4 should work well.void-the-void/
the author seems to suggest it may break sometimes and try never to use href="javascript:[anything]"
method 5 may work the best? according to the article above, that may be the best way as it doesn't use href="javascript:[something]"
and the return false part will cause the href="#" not to be evaluted, so that's the best way? thanks very much!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With