Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

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>

try it"


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>


try it"


<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>


try it"


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>

try it"

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.