Basic JavaScript knowledge

Source: Internet
Author: User

How to put JavaScript into an HTML page

If you need to insert a piece of JavaScript into an HTML page, we need to use the <script> tag (and use the type attribute to define the scripting language).

Cases:<script type="text/javascript">

          document.write("Hello World!");

         </script>
    
In this way, <script type= "Text/javascript" > and </script> can tell the browser where JavaScript starts and where it ends.



Different roles placed in different positions

    script in the head section: when the script is called, or when the event is triggered, the script is executed. When you put the script in the Head section, you can

Make sure that it is loaded before you need to use the script.

    script in the body section: The script is executed when the page is loaded. When you place the script in the body section, it generates the contents of the page.

    in the body and Head section of the script: you can put any number of scripts in the document, so you can both put the script into the body and can be placed in the head section.

    Using external JavaScript:

Sometimes, you might want to run JavaScript on several pages without writing the same script on each page.

For this purpose, you can write JavaScript into an external file. Then save the file with a. js suffix.

Note: External files cannot contain <script> tags.

You can then use this external file by assigning the. js file to the "src" attribute in the <script> tag:

Cases:<script src="xxx.js">....</script>

How to deal with an old browser

Browsers that do not support JavaScript will display the script as the content of the page. To prevent this from happening, we can use such HTML comment tags:

          

<body> <script type="text/javascript">               <!--               document.write("Hello World!");               //-->               </script> </body> 
The two forward slash at the end of the comment line is a JavaScript comment symbol that prevents the JavaScript compiler from compiling the line.

Output statement

    The document.write field is a standard JavaScript command used to write output to a page.

It is best to add a semicolon ";", the semicolon is optional according to the criteria, but it is best to add.

Multiple JavaScript statements within a line are separated by semicolons.

Combining code Blocks

JavaScript uses a pair of curly braces to combine blocks of code. The function of a code block is to execute a sequence of statements together. Often used in functions or conditional statements.

JavaScript comments

There are single-line comments '//' and multiline comments '/**/' in JavaScript, using the same method as comments in Java.

JavaScript variables

Rules for JavaScript variable names:

Variables are case sensitive (Y and Y are two different variables)

The variable must start with a letter or an underscore

        Note: Because JavaScript is case sensitive, variable names are also case-sensitive.

    To declare (create) a JavaScript variable:

Declare variables using var:

            var carname;

Declare and assign values:

            var carname= "Volvo";

To assign a value to a JavaScript variable:

        Carname= "Volvo";

    To assign a value to an undeclared JavaScript variable:

If the assigned variable has not been declared, the variable is automatically declared.

        Carname= "Volvo"; and var carname= "Volvo"; The effect of the statement is the same.

To re-declare the JavaScript variable:

If you declare a JavaScript variable again, the variable does not lose its original value:

            var x=5;

var x;

After the above statement is executed, the value of the variable x is still 5. When the variable is re-declared, the value of x is not reset or cleared.

JavaScript operators

Most operators in JavaScript use the same methods and rules as the operators in Java. The following are not the same:

    ' = = = ', congruent (value and type) compares values for equality, and compares data types for equality.

Basic JavaScript knowledge

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.