1. Introduction to JavaScript
JavaScript History Review
What is JavaScript?
The relationship between JavaScript and ECMAScript
Different versions of JavaScript
In a word, a little.
2. Using JavaScript in HTML
To put JavaScript on a Web page involves the Web's core language, HTML. One of the key issues to solve when developing JavaScript was to have JavaScript coexist with HTML pages without affecting the rendering of the page in the browser. Finally decided to add unified scripting support for the Web.
2.1 <script> Elements
The <script> element can contain either a script statement or an external script file via the SRC attribute.
<script> defines 6 properties:
- async:[Optional]. Indicates that the script should be downloaded immediately, but should not interfere with other actions on the page, such as downloading additional resources or waiting for other scripts to load. This property is valid only for external script files.
- charset:[Optional]. Represents the character set of the code specified by the SRC attribute, which is seldom used.
- defer:[Optional]. Indicates that the script can be deferred until the document is fully parsed, and that the property is only valid for external script files.
- Language: Deprecated.
- src:[Optional]. Represents an external file to contain the execution code.
- type:[Optional]. Can be seen as an alternative attribute of language. Represents the content type, also known as the MIME type, of the scripting language that the code is written in. Although both Text/javascript and Text/ecmascript have not been recommended, people have been using text/javascript for a long time. In fact, the server is using a MIME type of application/x-javascript when it transmits the JavaScript file, but setting this value in type may cause the script to be ignored. In addition, Application/javascript and application/ecmascript can also be used in non-IE browsers. The current value of the type attribute is still text/, given the conventional and maximum browser compatibility Javascript. However, this property is not required, and its default value is Text/javascript.
Note
- When parsing embedded JavaScript code or an external JavaScript file (including downloading the file), the processing of the page is temporarily stopped.
- Do not appear anywhere in the code "<script>" string, the browser will think that is the end tag. Resolve this issue by escaping the character "\" ("<\/script>").
- If the JavaScript code is included in the <script> tag with the SRC attribute, only the external script file is downloaded and executed, and the embedded code is ignored.
- As long as the defer and async properties do not exist, the browser parses the <script> elements sequentially in the order they appear in the page.
2.1.1 The location of the label
In general, all <script> elements are placed in the
How did it break? Place the JavaScript reference behind the page content in the <body> element:
<body>
<!--page Content –>
<script type= "" src= "" ></script>
</body>
2.1.2 Delay Script
The src= attribute is set in <script defer= "defer" type= "defer" ></script>, indicating that the script executes without affecting the construction of the page, that is, the script is downloaded immediately, but the entire page resolution is completed before execution (deferred execution).
Although the above approach can put the <script> element in the
Given the different ways in which browsers are handled, it is still the best option to put the deferred script at the bottom of the page.
2.1.3 Asynchronous script
HTML5 defines the async attribute for <script>, which, like defer, is used to change the behavior of the script, as well as for external script files, and tells the browser to download the file immediately. However, unlike defer, scripts marked as Async are not guaranteed to be executed in the specified order of precedence. Therefore, it is important to determine the non-dependency between the script files. The purpose of specifying the async attribute is to not allow the page to wait for two scripts to download and execute, thus loading the page's other content asynchronously. For this reason, it is recommended that asynchronous scripts do not modify the DOM during loading. Asynchronous scripts must be executed before the Load event of the page, but may be executed before or after the domcontentloaded event is triggered.
2.2 <noscript> Elements
If the browser does not support scripting or browser support scripts but the script is disabled, the browser will display the contents of <noscript>.
If the browser supports scripting, it does not display the text in the NoScript element.
A browser that does not recognize <script> tags will display the contents of the label on the page. To prevent the browser from doing so, you should hide the script in the Comments tab. Old-Fashioned (unrecognized <script> tagged) browsers ignore annotations, so that the contents of the tag are not written on the page, and modern browsers know how to execute them, even if they are enclosed in a comment tag.
3. Basic Concepts
Each language is similar.
Strict mode : ECMAScript introduces the concept of strict mode, to enable strict mode throughout the script, you need to add at the top: "Use strict"; Yes, it's just a string! In strict mode, the results of JavaScript execution can vary greatly. Browsers that support strict mode include ie10+, firefox4+, safari5.1+, opera12+, and Chrome.
data type : Undefined, Null, Boolean, number, String, Object (essentially consisting of a set of required name-value pairs).
For the number type, you need to specify:
- The octal first digit is represented as zero, and if the value goes out of range (a number larger than 7) causes the first 0 to be ignored as a decimal resolution. eg:070 = 56, 079 = 79, 08 = 8. The use of octal literals is not valid in strict mode and causes the JavaScript engine of the pattern to throw an error.
- The first two bits of hexadecimal literals are 0x.
- 0.1+0.2=0.30000000000000004
- IsNaN (true) = False, number (true) = 1
- ECMASCRIPT3 under parseint ("070") =56,ecmascript5 is 70, to prevent this, you can use parseint ("070", 8), and the second parameter is a binary.
JavaScript-Introduction, using JavaScript in HTML, basic concepts