Easy to learn JavaScript three: The combination of JavaScript and HTML

Source: Internet
Author: User
Tags button type

JavaScript scripts in HTML must be between <script> and </script> tags, and JavaScript scripts can be placed on HTML pages

<body> in labels and

One <script> label

to insert a JavaScript script in an HTML page, use the <script> tag. <script> and </script> will tell JavaScript where to start

and end. The code line between <script> and </script> contains javascript:

<span style= "FONT-SIZE:18PX;" ><script type= "Text/javascript" >alert ("Welcome to JavaScript World!!!" ");</script></span>
you don't need to understand the code above. Simply understand that the browser interprets and executes the <script> and </script>JavaScript between the two. Those old

Old instance may use Type= "Text/javascript" in the <script> tag. It is not necessary to do so now. JavaScript is all modern browsers

in and the default scripting language in HTML5. Given that just learning JavaScript language can be used!

JavaScript in the second <body>

In this example, JavaScript writes text to the HTML <body> when the page loads:

Instance code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Let's look at the results of the JavaScript code first, regardless of how it is written and how it works:


Three JavaScript functions and events

The JavaScript statements in the example above will be executed when the page loads. In general, we need to execute code when an event occurs, such as when a user

When you click the button. If we put JavaScript code in a function, we can call it when the event occurs.

JavaScript in the Quad

You can put an unlimited number of scripts in the HTML document. The script can be in the <body> or

In two sections. It is common practice to put functions in the

Does not interfere with the content of the page.

JavaScript functions in the V

In this example, we put a JavaScript function in the

Instance code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The result of the operation is:


The effect after clicking the button is:


Javascrip functions in the six <body>

In this example, we put a JavaScript function in the <body> section of the HTML page. The function is called when the button is clicked:

Instance code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The result of the operation is the same as the above five results!

tip: We put JavaScript at the bottom of the page code to ensure that the script is executed after the <p> element is created.

Seven external JavaScript

We can also save the script to an external file. External files typically contain code that is used by multiple Web pages. File extension for external JavaScript files

The exhibition name is. js. To use an external file, set the. js file in the "src" attribute of the <script> tag, and if there is a lot of JavaScript code, I

We advocate the use of external JavaScript, and we typically connect to HTML documents in a separate way.

Instance

HTML code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Myscript.js Code:

function MyFunction () {document.getElementById ("demo"). Innerhtml= "My first JavaScript Function";}
the results of the operation are consistent with the above!

Tip: referencing a script file in

External scripts cannot contain <script> tags.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Easy to learn JavaScript three: The combination of JavaScript and HTML

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.