JavaScript Usage

HTML in the script must be located between the <script> and </ script> tag.

Script can be placed in the HTML page <body> and <head> section.


<Script> tag

To insert JavaScript in the HTML page, use the <script> tag.

<Script> and </ script> tells JavaScript where to begin and end.

Line of code <script> and </ script> between contains JavaScript:

<script>
alert("我的第一个 JavaScript");
</script>

You do not understand the above code. Just understand that the browser will interpret and execute JavaScript code is in <script> and </ script> between

lamp Examples of those old may use type = "text / javascript" in the <script> tag. Now you do not have to do so. JavaScript is all modern browsers and HTML5 default scripting language.


<Body> in JavaScript

In this example, JavaScript in the HTML page is loaded when the <body> write text:

Examples

<!DOCTYPE html>
<html>
<body>
.
.
<script>
document.write("<h1>这是一个标题</h1>");
document.write("<p>这是一个段落</p>");
</script>
.
.
</body>
</html>



JavaScript functions and events

JavaScript statements in the above example, executed when the page loads.

In general, we need to execute code when an event occurs, such as when the user clicks the button.

If we put JavaScript code in the function, you can call the function when the event occurs.

You will learn more about the knowledge of JavaScript functions and events in later chapters.


In the <head> or <body> of JavaScript

You can place an unlimited number of scripts in an HTML document.

HTML script is located in the <body> or <head> section, or exist in two sections.

The usual practice is to function into the <head> section, or on the bottom of the page. So that they can be placed to the same place to which does not interfere with the content of the page.


<Head> JavaScript functions

In this case, we have a JavaScript function placed in HTML pages <head> section.

This function is called when the button is clicked:

Examples

<!DOCTYPE html>
<html>

<head>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="我的第一个 JavaScript 函数";
}
</script>

</head>

<body>

<h1>我的 Web 页面</h1>

<p id="demo">一个段落</p>

<button type="button" onclick="myFunction()" >尝试一下</button>

</body>
</html>




<Body> JavaScript functions

In this case, we have a JavaScript function placed in the <body> HTML page.

This function is called when the button is clicked:

Examples

<!DOCTYPE html>
<html>
<body>

<h1>我的 Web 页面</h1>

<p id="demo">一个段落</p>

<button type="button" onclick=" myFunction() ">尝试一下</button>

<script>
function myFunction()
{
document.getElementById("demo").innerHTML="我的第一个 JavaScript 函数";
}
</script>

</body>
</html>




External JavaScript

You can also save the script to an external file. External files usually contain code to be used by multiple pages.

File extension is external JavaScript file .js.

To use an external file, set the .js file in the <script> tag is "src" attribute:

Examples

<!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>

You can place the script in the <head> or <body> in the actual operating results and you write the script exactly the same <script> tag.

myScript.js file code is as follows:

function myFunction()
{
    document.getElementById("demo").innerHTML="我的第一个 JavaScript 函数";
}
lamp External script can not contain the <script> tag.