15

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?

5
  • 3
    Check it out: stackoverflow.com/questions/134845/… Commented Apr 23, 2014 at 9:40
  • 1
    1. The purpose is to have an anchor element with no default HTML action 2. yes 3. yes 4. no, because you can still use the anchor with (for example) onclick event handlers or click event listeners Commented Apr 23, 2014 at 9:42
  • Regarding the last bullet point: How can "Having an <a>" possibly be the same as "Not having an <a>"? Commented Apr 23, 2014 at 9:43
  • @Cerbrus: In the same way that <div>text</div> is the same as text, except for any CSS implications Commented Apr 23, 2014 at 9:54
  • Thank you for the suggestions of duplicate questions, but neither address href="javascript:;". If it is the same as href="#" or href="javascript:void(0)" then please write that as an answer, but I see no duplicate of this specific question Commented Apr 23, 2014 at 10:03

1 Answer 1

6

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!

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. Where does <a href="javascript:;"></a> fit into this scheme?
href="" will link to the same page as the one you are currently on, effectively refreshing the page. href="#" will not refresh the page, but using the # will make the screen move to the top of the page (it is the browser effectively looking for an anchor with no name, ). javascript:void(0) will prevent anything happening on the link at all.
Thank you again, but you haven't addressed my question about href="javascript:;"
When we create a link for solve our purpose to run JavaScript code, there are 2 ways to write the code. Which is better, in terms of functionality, page load speed, validation purposes, etc? <a href="#" onclick="myJsFunc();">Run JavaScript Code</a> <a href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>
I understand, but you still haven't mentioned href="javascript:;". Where does it fit into this scheme?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.