JavaScript Code Inside <script> Tag

…衆ロ難τιáo~ 提交于 2019-12-01 17:55:13

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

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

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);

The browser will ignore any content in <script src /> tag.

However, the Firebug Lite Javascript will specifically find its <script> tag and parse the content manually.

Here is the code in question that parses the JSON object in case anyone is interested.

// 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!