The script in HTML must be located between the script and/script tags. Scripts can be placed in the body and head of the HTML page. To insert JavaScript into an HTML page, use the script tag. Scripts and/scripts tell JavaScript where to start and end. Script and/script
The script in HTML must be located between the script tag and the script tag.
Scripts that can be placed on HTML pagesAnd.
Script tag
To insert JavaScript into the HTML page, use the script tag.
Script and script tell JavaScript where to start and end.
The code lines between script and script contain JavaScript:
Script
Alert ("My First JavaScript ");
Script
You do not need to understand the above Code. You only need to understand that the browser will interpret and execute JavaScript between script and script.
Old instances may use type = "text/javascript" in the script tag ". You don't have to do this now. JavaScript is the default scripting language in all modern browsers and HTML5.
In JavaScript
In this example, JavaScript loadsWrite text:
Instance
.
.
Script
Document. write ("This is a heading ");
Document. write ("
This is a paragraph
");
Script
.
.
Try it yourself
JavaScript Functions and events
The JavaScript statement in the preceding example is executed during page loading.
Generally, we need to execute code when an event occurs, for example, when a user clicks a button.
If we put JavaScript code into a function, we can call this function when an event occurs.
You will learn more about JavaScript Functions and events later.
OrIn JavaScript
You can add an unlimited number of scripts in the HTML document.
Scripts can be located in HTMLOrOr both.
The common practice is to put the functionOr at the bottom of the page. In this way, you can place them in the same location without interfering with the page content.
JavaScript Functions in
In this example, we place a JavaScript function in.
This function will be called when you click the button:
Instance
Script
Function myFunction ()
{
Document. getElementById ("demo"). innerHTML = "My First JavaScript Function ";
}
Script
My Web Page
A Paragraph
> Try it
Try it yourself
JavaScript Functions in
In this example, we place a JavaScript function in.
This function will be called when you click the button:
Instance
My Web Page
A Paragraph
> Try it
Script
Function myFunction ()
{
Document. getElementById ("demo"). innerHTML = "My First JavaScript Function ";
}
Script
Try it yourself
Tip: We put JavaScript at the bottom of the page Code to ensure that
The script is executed after the element is created.
External JavaScript
You can also save the script to an external file. An external file usually contains code used by multiple webpages.
The extension of an external JavaScript file is. js.
To use an external file, set the. js file in the "src" attribute of the script Tag:
Instance
Script
Try it yourself
InOrYou can reference the script file in. The actual running effect is exactly the same as that of writing scripts in the script tab.
Tip: External scripts cannot contain the script tag.