JavaScript代码内部< script>标签 [英] JavaScript Code Inside <script> Tag

查看:107
本文介绍了JavaScript代码内部< script>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,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文档中注意到它:
http://getfirebug.com/firebuglite#ScriptJSONOptions

I noticed it here in the firebug lite documentation: http://getfirebug.com/firebuglite#ScriptJSONOptions

推荐答案

由于元素具有 src 属性,因此不会执行内容。它不是严格合法的。 HTML5规范说:

The content is not executed because the element has a src attribute. It's not strictly legal as is. The HTML5 spec says:


如果有 src 属性,则元素必须为要么为空,要么只包含也符合脚本内容限制的脚本文档。

If there is a src attribute, the element must be either empty or contain only script documentation that also matches script content restrictions.

< 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代码内部&lt; script&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆