What's the difference between "javascript:;" and "javascript:" in an anchor href
attribute?
Same as the difference between empty Javascript file and Javascript file with just a ;
.
Nothing:
eval("");
//undefined
eval(";");
//undefined
See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
When, as the program is parsed from left to right, the end of the input stream of tokens is encountered and the parser is unable to parse the input token stream as a single complete ECMAScript Program, then a semicolon is automatically inserted at the end of the input stream.
So, empty file would be an invalid Program, then semicolon is inserted automatically, and it becomes equal to a Program with just a semicolon.
It just occurred to me that this is yet another case that prooves JSON is not a subset of Javascript: empty JSON is not valid:
JSON.parse("");
//SyntaxError: Unexpected end of input
eval("");
//undefined
:P
javascript:
indicates the pseudo-protocol that can be used to evaluate JavaScript. So a single semicolon after it is equal to a script containing just ;
which is an empty expression that does nothing. javascript:
without anything else after it is an empty script that also does nothing. In both cases the return values are undefined
which is important since a javascript:
url returning something else would result in the page contents being replaced with whatever it returned.
However, you should not use javascript:
urls at all - they are deprecated. Use onclick
and either a useful href or #
if there is no non-js version of the link. Remember to preventDefault the event in that case though.
javascript:
tells that there is a javascript statement coming rather than a link to another page. The ;
is the javascript statement. However, ;
won't execute anything, so this is a no-op.
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