[JavaScript tutorial] JavaScript usage
JavaScript usage
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 the JavaScript code 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 title"); document. write ("This is a paragraph
"); Script...
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.
InOrJavaScript
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";} scriptMy Web pageOne paragraph
Try it
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 pageOne paragraph
Try itScript function myFunction () {document. getElementById ("demo"). innerHTML = "My first JavaScript function";} script
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