Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Code Inside <script> Tag

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>

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

like image 966
user996015 Avatar asked Feb 04 '12 00:02

user996015


2 Answers

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);
like image 109
Mike Samuel Avatar answered Oct 16 '22 21:10

Mike Samuel


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.

like image 21
SLaks Avatar answered Oct 16 '22 23:10

SLaks