Use Javascript in HTML

Source: Internet
Author: User

Address: http://www.qeefee.com/js/use-javascript-in-html

For more JavaScript tutorials, visit> JavaScript tutorials

Body:

You can use JavaScript on an HTML page in either of the following ways:

Embedded Javascript Code

In this way, JavaScript code is directly written in HTML. The corresponding usage is:

 
<! Doctype HTML> <HTML xmlns = "http://www.w3.org/1999/xhtml">  

In the above Code, we put the script tag containing the JavaScript code in the header. When we run the page, a dialog box will pop up, such:

In addition to the header, JavaScript code can also be placed in the body:

<Body>  

We add a script tag to the body and let it pop up a dialog box. When the browser loads this code, it will explain and execute it. The effect is as follows:

Both writing methods can be executed. There is no distinction between recommendation and not recommended writing, and there is no difference between correctness and error. It mainly depends on your specific usage: if you want the page to start executing JavaScript code at the beginning of loading, you should put it in the head; if you want to execute JavaScript code after loading the page to a tag, put it behind the corresponding tag.

Reference independent JS files

It is convenient to embed Javascript code in HTML tags, but it also easily leads to Page code confusion. Normally, we store JavaScript code in a JS file, then reference the JS file in HTML as follows:

It should be noted that loading external JS files is thread-blocking. When loading is not completed, the page will not continue to load the labels behind it, so we usually put the referenced JS behind the body.

Finally, download this example, hoping to help beginners!

Use javascript .rar

Related Article

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.