Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "javascript:;" and "javascript:" in href attribute?

What's the difference between "javascript:;" and "javascript:" in an anchor href attribute?

like image 331
dhblah Avatar asked Apr 17 '13 09:04

dhblah


3 Answers

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

like image 117
Esailija Avatar answered Nov 19 '22 10:11

Esailija


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.

like image 39
ThiefMaster Avatar answered Nov 19 '22 09:11

ThiefMaster


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.

like image 1
Jan Jongboom Avatar answered Nov 19 '22 09:11

Jan Jongboom