问题描述
显然,JSON对象可以在链接脚本中传递。我正在试图弄清楚它是如何工作的(或者如果它有效):
Apparently a JSON object can be passed inside a linked script. I'm trying to figure out exactly how this works (or if it does):
<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js">
{
overrideConsole: false,
startInNewWindow: true,
startOpened: true,
enableTrace: true
}
</script>
我在firebug lite文档中注意到它:
I noticed it here in the firebug lite documentation:http://getfirebug.com/firebuglite#ScriptJSONOptions
推荐答案
由于元素具有 src
属性,因此不会执行内容。它不是严格合法的。 说:
The content is not executed because the element has a src
attribute. It's not strictly legal as is. The HTML5 spec says:
< script>的内容;
元素既不是有效的JSON也不是有效的JavaScript。它不是有效的JSON,因为未引用属性名称。它不是有效的JavaScript,因为虽然它看起来像带有标记语句的块表达式,但 startInNewWindow
之后的冒号不能合法地出现在那里。
The content of that <script>
element is neither valid JSON nor valid JavaScript. It is not valid JSON because the property names are not quoted. It is not valid JavaScript because, although it looks like a block expression with labeled statements, the colon after startInNewWindow
cannot legally appear there.
也就是说,加载的脚本总是可以查找最后一个脚本元素并解析其内容:
That said, the script that is loaded can always look for the last script element and parse its content:
var scripts = document.getElementsByTagName('SCRIPT');
var lastScript = scripts[script.length - 1];
var content = eval(lastScript.innerText || lastScript.textContent);
这篇关于JavaScript代码内部< script>标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!