JavaScript code inside the <script> tag

Apparently, a JSON object can be passed inside the associated script. I am trying to figure out how this works (or if so):

<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"> { overrideConsole: false, startInNewWindow: true, startOpened: true, enableTrace: true } </script> 

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

+4
source share
3 answers

Content is not executed because the element has an src attribute. It is not strictly legal as it is. HTML5 specification says:

If the src attribute exists, this element must be empty or contain only script documentation that also meets the script's content limits.

The content of this <script> element is not valid JSON or valid JavaScript. This is not valid JSON because property names are not quoted. It is not valid JavaScript because, although it looks like a block expression with marked instructions, the colon after startInNewWindow cannot legally appear there.

However, a loadable script can always search for the last element of a script and parse its contents:

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

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

However, Firebug Lite Javascript specifically finds its <script> and parses the content manually.

+5
source

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

+1
source

Source: https://habr.com/ru/post/1394717/


All Articles