JavaScript< href>的返回值应该如何?链接被处理? [英] How should the return value of JavaScript <href> links be handled?
问题描述
href
链接的返回值的正确方法是什么? 一些例子:
< a href ='javascript:Hello World;'>点击< / a> <! - return a String - >
< a href ='javascript:ThisFunctionReturnsString();'>点击< / a>
snippetdata-lang =jsdata-hide =true>
< a href ='javascript:12345;'>点击< / a> <! - Number - >
< a href ='javascript:[1,2,3,4,5];'>点击< / a> <! - 数组 - >
< a href ='null;'>点击< / a> <! - null - >
< a href ='undefined;'>点击< / a> <! - undefined - >
< a href ='javascript:{};'>点击< / a> <! - Object - >
标准 标准的浏览器应该如何处理返回值?
当前的浏览器如何与这种标准行为有所不同?
w3.org/html5/spec-LC/webappapis.html#javascript-protocolrel =nofollow> Web应用程序API§6.1.5:下面解释了为什么单击链接替换文档内容:
如果执行脚本的结果是void(没有返回
值),那么URL必须以HTTP HTTP $无内容响应的HTTP
资源的方式进行处理。
否则,必须以相当于具有200 OK响应的HTTP
资源的方式处理URL,其内容类型元数据为
text / html,其响应主体为retu rn值转换为
字符串值。
这种行为也可以通过简单粘贴 javascript:Hello World;
在地址栏中。同样的 javascript:(function(){returnHello World;})()
。
以下解释了为什么只有您的1和2代码片段实际上在做某些事情。
让脚本源代码为 strong>使用为javascript定义的内容检索操作获得:URLs
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
这篇关于JavaScript< href>的返回值应该如何?链接被处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!