A comprehensive understanding of script tags in js

  • 2021-07-01 06:27:03
  • OfStack

Introduce the javascript file with the script tag in the page ( < script type= "text/javascript" src= "js File Address" > < /script > ), when the browser is rendering the page, when reading the script element, the browser will not process its contents in the way of HTML or XHTML, and the browser will notify the browser's script engine to take over the contents of the script element.

The type attribute of the script element defines the script type, and the type types are:

1. text/ecmascript (means parsing this script in ECMAScript mode, that is, based on ECMA-262 script standard)

2. text/jscript (means parsing this script in JScript mode, which is a variant of ECMAScript language implemented by Microsoft in IE browser)

3.text/vbscript

4.text/vbs

(3, 4 representations are handled in Microsoft's VBScript way, and are completely different scripting languages.)

The language attribute of the script element (its attribute was used in earlier script tags to solve browser compatibility problems):

< script type="text/javascript" src="a.js" language="javascript1.2" > < /script >

language defines the browser version that supports the script (that is, if the browser supports javascript 1.2, the code in the a. js file is executed)

defer attribute of script element:

< script type="text/javascript" src="a.js" language="javascript1.2" defer="defer" > < /script >

The defer property is set to "defer", which means that the script will not generate any document content, so the browser can process the rest of the page ahead of time, and only process the script part when the page is finished and ready to display.

Where the script tag is placed on the page.

There are limits to the resources that browsers can load concurrently from the same domain name, so when a script is added to an head element, the script will be loaded first, followed by the rest of the document. Why is it that putting the script element tag with javascript in head may cause the browser to delay the rest of the page? This is mainly because the dcument object may be modified by calling the document. write method in the script.

Reference: Javascript Learning Guide


Related articles: