Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a script-Tag with src AND content mean?

Tags:

javascript

People also ask

What is src in script tag?

The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again.

What does script tag mean?

Definition and Usage The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

Where do I put script src in HTML?

Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.


Different browsers treat this differently. Some run the content only if the src is included without error. Some run it after attempting to include the src script, regardless of success. Since this behaviour is unreliable (and prohibited in HTML5), it should be avoided.

Google isn't relying an any specific behaviour. Since the content is just an object literal (a value), executing it would not actually do anything except cause a silent error. Google's code looks at the contents of the script tag itself, and adjust its behaviour based on that.


If a script element has a src attribute, the content must be ignored, any other behaviour is non-conformant.

It has been suggested in blogs (as a hack) to put content in the element knowing that it won't be evaluated, then use DOM methods to get the content as a string and either eval it or insert it in a new script element. Neither of these are a good idea.


After the script has loaded, it looks inside its own script tag to access its content.

It will use some code similar to this:

var scripts = document.getElementsByTagName("script");
var data = eval(scripts[scripts.length - 1].innerHTML);

Courtesy of John Resig.


According to the HTML5 draft specification, <script> elements with src attributes should only have commented-out code, which is intended to give documentation for the script. It doesn't appear as though Google is conforming to this specification though.