Ways to insert JavaScript into a Web page

Source: Internet
Author: User

Ways to insert JavaScript into a Web page

Use javascript code.

inserting javascript

Similar to how you insert CSS in a Web page . Use the following code to insert JavaScript in a Web page:

...

One of the ... Is the content of the code. JavaScript can be inserted in different locations, the effects will vary, and as with CSS, JavaScript will be saved as an external file, which will be discussed in the next section.

Output content in a Web page with JavaScript

JavaScript uses document.write to output content. For example

document.write (" I am a rookie I fear who !");

Output will be output on the Web page:

I'm a rookie, I'm afraid of who !

People who have studied programming should know,"I'm a rookie, I'm afraid of who !" Double quotes on both sides represent the meaning of the string. But it doesn't matter if you don't understand the concept, you'll know when you should add double quotes and when you don't need to add them.

Hide code for browsers that do not support JavaScript

Some browsers may not support JavaScript, and we can hide JavaScript code from them using the following methods .

Content is equivalent to a comment for browsers that do not support JavaScript, and This code will still execute for JavaScript-enabled browsers. The "//" symbol is a comment symbol in JavaScript, which is added here to prevent JavaScript from trying to execute . In general, however, browsers now support almost all JavaScript, even if they are not, and understand how to properly handle Web pages that contain JavaScript.

Where to insert JavaScript

JavaScript scripts can be placed in the head of a Web page or in the body part, and the effect is different.

JavaScript in the body

The JavaScript script placed in the body section executes when the Web page reads to the statement, for example:

JavaScript in the head

The script in the head section executes when it is called, for example:

....

Add an external JavaScript script

You can also add an external JavaScript script file as you would add an external css , which is usually a . js prefix. For example:

Writing this code to an external JavaScript file is the best approach if many pages need to contain the same code. Thereafter, any page that needs this feature, just introduce this js file.

Note: The script file can no longer contain

Note: the function placed in the body is an exception, it will not be executed, but will not be executed until it is called. The concept of functions and invocations will be described later.

Ways to insert JavaScript into a Web page

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.