简体   繁体   English

JavaScript应如何返回值 <href> 链接被处理?

[英]How should the return value of JavaScript <href> links be handled?

According to standards , what's the correct way to handle the return value of javascript protocol href links? 根据标准 ,处理javascript协议href链接的返回值的正确方法是什么?

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 : Web应用程序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. 如果执行脚本的结果为空(没有返回值),则必须以等效于带有HTTP 204 No Content响应的HTTP资源的方式来处理URL。

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 . 否则,必须以与具有200 OK响应的HTTP资源等效的方式对待URL,该响应的Content-Type元数据为text / html,其响应主体为转换为字符串值的返回值

This behavior can also be demonstrated easily by simply pasting javascript:"Hello World"; 通过简单地粘贴javascript:"Hello World";也可以很容易地证明这种行为javascript:"Hello World"; in the address bar. 在地址栏中。 Same goes for javascript:(function() { return "Hello World";})() . javascript:(function() { return "Hello World";})()

And the following explains why only your 1 and 2 code snippets are actually doing something. 以下内容说明了为什么只有您的1和2代码段实际上在做某事。

Let the script source be the string obtained using the content retrieval operation defined for javascript: URLs 让脚本源为使用为javascript:URL定义的内容检索操作获得的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM