The best JavaScript Usage Tutorial In 2024, In this tutorial you can learn <Script> tag,<Body> in JavaScript,Examples,JavaScript functions and events,In the <head> or <body> of JavaScript,<Head> JavaScript functions,Examples,<Body> JavaScript functions,Examples,External JavaScript,Examples,
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.
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:
You do not understand the above code. Just understand that the browser will interpret and execute JavaScript code is in <script> and </ script> between
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. |
In this example, JavaScript in the HTML page is loaded when the <body> write text:
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.
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.
In this case, we have a JavaScript function placed in HTML pages <head> section.
This function is called when the button is clicked:
<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>
In this case, we have a JavaScript function placed in the <body> HTML page.
This function is called when the button is clicked:
<h1>我的 Web 页面</h1>
<p id="demo">一个段落</p>
<button type="button" onclick=" myFunction() ">尝试一下</button>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="我的第一个 JavaScript 函数";
}
</script>
</body>
</html>
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:
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 函数"; }
External script can not contain the <script> tag. |