Using JavaScript in HTML

Source: Internet
Author: User
Tags script tag

There are two ways to use JavaScript in an HTML page:

Embedded JavaScript code

In this way, the JavaScript code is written directly in HTML, and the corresponding usage is:

<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/>    <title>Using JavaScript in HTML-take-off web-javascript test page</title>    <Scripttype= "Text/javascript">Alert ("Welcome to JavaScript-take-off network"); </Script></Head><Body></Body></HTML>

In the above code, we include the script tag of the JavaScript code in and head, and when we run the page, a dialog box pops up, such as:

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

<Body>    <H1>Using JavaScript in HTML-take-off network</H1>    <Scripttype= "Text/javascript">        //the Javascrip code embedded in the bodyAlert ("This is the JavaScript in the body-takeoff net"); </Script></Body>

We add a script tag to the body and let it pop up a dialog box that will be interpreted when the browser is loaded into the code, such as:

Both types of writing can be executed, there is no recommendation and not recommended to the writing of the points, but not correct and wrong, mainly to see 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 the page is loaded into a tag, place it behind the corresponding label.

Referencing a standalone JS file

It is convenient to embed JavaScript code in HTML tags, but it is also easy to cause page code confusion, usually, we will put the JavaScript code in a JS file, and then reference the JS file in HTML, the following wording:

It is important to note that the loading of external JS files is a thread blocking, when the loading is not completed, the page will not continue to load the tag behind it, so we usually put the reference JS behind the body.

Finally give the download of this example, hope to help beginners friends!

Takeoff nets-use JavaScript examples in HTML. rar

Using JavaScript in HTML

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.