JavaScript (2) code placement and execution sequence

Source: Internet
Author: User

  1. Relationship between JavaScript and JScript

    1. In the past, JavaScript and JScript were the scripting languages designed and developed by Netscape and Microsoft for their browsers, many web programmers have to design different scripting languages for different browsers. They are very worried. To solve the same script problem, ecmascript was born, an international standard JavaScript version. Currently, mainstream browsers support this version. Now the word JavaScript also represents the meaning of these two standards, so it is used.
  2. Javascript development environment and editing tools
    1. Its running environment is very simple. You only need a browser to run JavaScript code.
    2. There are also many types of editing work, such as the common ps3, dreamwave, VS, notepad, and so on.
  3. Basic JavaScript Architecture
    1. <Head> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "/> <title> first JavaScript Program </title> 

      Above <! --...... --> To solve browser incompatibility, if the old browser is not compatible with JavaScript code, the JavaScript code will be hidden; otherwise, it will be viewed as part of HTML code displayed in the browser, it is called useless information.

  4. JavaScript code location

4.1 JavaScript code in the head area

The javascript code in the head area is used to ensure that the relevant function code has been loaded before the program code is called, because the code is executed from top to bottom, generally, the code in this region is the event or processing function called by the code in the body region. The code execution result is as follows:

<Title> first JavaScript Program </title> <script language = "JavaScript"> // hide comments <! -- Function showmessage () {alert ("JavaScript code in the head area") ;}></SCRIPT> 

The execution result is:


When the pop-up box is displayed, the body content is displayed, that is, this part of the code has been loaded, and the body part of the code is loaded before the head area.

4.2 JavaScript code in the head area

<! -- Load first --> <link href = "CSS/admin.global.css" rel = "stylesheet" type = "text/CSS"/> <link href = "CSS/admin.index.css" rel =" stylesheet "type =" text/CSS "/> <! -- Post-load --> <SCRIPT src = "JS/jquery-1.4.2.min.js" type = "text/JavaScript"> </SCRIPT> <SCRIPT src = "JS/jquery. utils. JS "type =" text/JavaScript "> </SCRIPT> <link href =" jbox/skins/green/jbox.css "rel =" stylesheet "type =" text/CSS "/>

The above link to the CSS file or JS file is loaded in the order from top to bottom. If a file needs another file, it needs to be placed at home before the file; otherwise, an error occurs, this is also a problem we often encounter. I hope you will pay more attention to it.

5 JavaScript code in the body area

When a webpage is loaded, the program code in the body area runs immediately. The content output by these codes is part of the webpage. See the following code.

<Body> <H2> JavaScript code in the body area </H2> <HR> <script language = "JavaScript"> // hide annotation text <! -- Alert ("JavaScript code in the body area"); --> </SCRIPT> run the event handler in the body area </body>

The execution result is:


In this execution result, the code after the body is <JavaScript> is not executed, indicating that the code in the body is executed immediately when the webpage is loaded, rather than when it is called, therefore, functions or code that need to call or handle events should be placed in the head area.

In addition, if You unmount the calling code from the body, because all the operations we do are part of a webpage, you also need to write them inside the webpage.

6. Writing Style

The javascript writing style has been described in the previous blog. Here, you need to note that the braces {} are not followed by the ";" symbol.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.