How to insert js, refer to external js, js position in the page

We look at how to write JS code? You only need one step, use the <script> tag into the JavaScript code in the HTML page. Note, <script> tag to appear in pairs, and to write JavaScript code between <script> </ script>. In particular, note that <script type = "text / javascript"> said in between <script> </ script> is the text type (text), javascript to tell the browser which text belongs to the JavaScript language.

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <title>javascript插入页面</title>
 6         <script type="text/javascript">
 7             document.write("hello javascript")
 8         </script>
 9     </head>
10     <body>
11     </body>
12 </html>

JavaScript code can be written in HTML files? Of course not, we can separate HTML files and JS code, and create a separate JavaScript file (referred to as JS file), usually its file extension .js, then the JS code directly write in the JS file; in the JS file, no <script> tags, JavaScript code written directly on it. Of course, this can not be directly run a JS file, just as the introduction of css external file, we need to need to embed js files to HTML file execution, we need to add in the HTML <script src = "js file path" type = "text / javascript "charset =" utf-8 "> </ script>, JS file can be embedded in an HTML file.

 1 <!DOCTYPE html>
 2 <html lang="zh">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 7     <title>javascript外部文件引入</title>
 8     <script src="demo2.js" type="text/javascript" charset="utf-8"></script>
 9 </head>
10 <body>
11 </body>
12 </html>

Js file was inserted into writing

1  // write js code
 2 document.write ( "I am demo2 file")

We can place JavaScript code in html file in any position, but we are generally on the head or body section of the page.
On the <head> section: The most common way is to place <script> element in the head section of the page, the browser parses the code it will execute this head portion before the rest of the page is parsed.
On the <body> section: JavaScript code for the page to read the statement will be executed when.
☆: javascript as a scripting language can be placed anywhere in html page, but when the browser interprets html is in chronological order, so the front on the first script to be executed. For example, for page display initialization js must be placed inside the head, because the initialization are required in advance (such as page body to set css, etc.); and if it is performed by an event calling function so no requirements for the position. => Here may explain " Why study javascrip T" in <script type = "text / javascript "> {document.write ( " try, and maybe possible"); document.getElementById ( "box2" ) .style.background = "red";} Why </ script> js is placed in the head tag, file error

Guess you like

Origin www.cnblogs.com/dhnblog/p/12459842.html