According to standards, what's the correct way to handle the return value of javascript protocol href
links?
Some examples:
<a href='javascript:"Hello World";'> Click </a> <!-- return a String -->
<a href='javascript:ThisFunctionReturnsString();'> Click </a>
<a href='javascript:12345;'> Click </a> <!-- Number -->
<a href='javascript:[1, 2, 3, 4, 5];'> Click </a> <!-- Array -->
<a href='null;'> Click </a> <!-- null-->
<a href='undefined;'> Click </a> <!-- undefined-->
<a href='javascript:{};'> Click </a> <!-- Object -->
How should a standard-compliant browser handle the return values?
How do current browsers in the wild differ from this standard behavior?
From Web Applications API §6.1.5:
The following explains why clicking the links replaces the document content:
If the result of executing the script is void (there is no return
value), then the URL must be treated in a manner equivalent to an HTTP
resource with an HTTP 204 No Content response.
Otherwise, the URL must be treated in a manner equivalent to an HTTP
resource with a 200 OK response whose Content-Type metadata is
text/html and whose response body is the return value converted to a
string value.
This behavior can also be demonstrated easily by simply pasting javascript:"Hello World";
in the address bar. Same goes for javascript:(function() { return "Hello World";})()
.
And the following explains why only your 1 and 2 code snippets are actually doing something.
Let the script source be the string obtained using the content retrieval operation defined for javascript: URLs