JavaScript代码内部标记

显然,JSON对象可以在链接脚本中传递。 我正在试图弄清楚它是如何工作的(如果它有效):

 { overrideConsole: false, startInNewWindow: true, startOpened: true, enableTrace: true }  

我在firebug lite文档中注意到了它: http : //getfirebug.com/firebuglite#ScriptJSONOptions

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

如果存在src属性,则该元素必须为空或仅包含也与脚本内容限制匹配的脚本文档。

元素的内容既不是有效的JSON,也不是有效的JavaScript。 它不是有效的JSON,因为未引用属性名称。 它不是有效的JavaScript,因为虽然它看起来像带有标记语句的块表达式,但startInNewWindow之后的冒号不能合法地出现在那里。

也就是说,加载的脚本总是可以查找最后一个脚本元素并解析其内容:

  var scripts = document.getElementsByTagName('SCRIPT'); var lastScript = scripts[script.length - 1]; var content = eval(lastScript.innerText || lastScript.textContent); 

浏览器将忽略标记中的任何内容。

但是,Firebug Lite Javascript将专门找到其标记并手动解析内容。

以下是有问题的代码,在有人感兴趣的情况下解析JSON对象。

 // process the Script JSON Options var innerOptions = FBL.trim(script.innerHTML); if (innerOptions) { var innerOptionsObject = eval("(" + innerOptions + ")"); for (var name in innerOptionsObject) { var value = innerOptionsObject[name]; if (name == "debug") { Env.isDebugMode = !!value; } else if (name in Env.Options) { Env.Options[name] = value; } else { Env[name] = value; } } } 

http://code.google.com/p/fbug/source/browse/lite/branches/firebug1.5/build/firebug-lite-debug.js#478