JavaScript Where To
In HTML, JavaScript code is inserted between and tags.
The Tag
In HTML, JavaScript code is inserted between and tags.
Example
Example Code
htmldocument.getElementById("demo").innerHTML = "My First JavaScript";JavaScript Functions and Events
A JavaScript function is a block of JavaScript code, that can be executed when "called" for.
For example, a function can be called when an event occurs, like when the user clicks a button.
JavaScript in or **
You can place any number of scripts in an HTML document.
Scripts can be placed in the , or in the section of an HTML page, or in both.
JavaScript in
In this example, a JavaScript function is placed in the section of an HTML page.
The function is invoked (called) when a button is clicked:
Example
Example Code
htmlfunction myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
## Demo JavaScript in Head
A Paragraph
Try
itJavaScript in
In this example, a JavaScript function is placed in the section of an HTML page.
The function is invoked (called) when a button is clicked:
Example
Example Code
html## Demo JavaScript in Body
A Paragraph
Try
it
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}External JavaScript
Scripts can also be placed in external files:
External file: myScript.js
Example Code
javascriptfunction myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}External scripts are practical when the same code is used in many different web pages.
JavaScript files have the file extension .js.
To use an external script, put the name of the script file in the src (source) attribute of a tag:
Example
Example Code
htmlYou can place an external script reference in or as you like.
The script will behave as if it was located exactly where the tag is located.
External JavaScript Advantages
Placing scripts in external files has some advantages:
- It separates HTML and code
- It makes HTML and JavaScript easier to read and maintain
- Cached JavaScript files can speed up page loads
To add several script files to one page - use several script tags:
Example
Example Code
htmlExternal References
An external script can be referenced in 3 different ways:
- With a full URL (a full web address)
- With a file path (like /js/)
- Without any path
This example uses a full URL to link to myScript.js:
Example
Example Code
htmlThis example uses a file path to link to myScript.js:
Example
Example Code
htmlThis example uses no path to link to myScript.js:
Example
Example Code
htmlVideo: JavaScript Where to?
Interactive Code Examples
JS Example 1
html<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>JS Example 2
htmlJS Example 3
htmlExternal file: myScript.js
javascriptfunction myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}JS Example 5
html<script src="myScript.js"></script>JS Example 6
html<script src="myScript1.js"></script>
<script src="myScript2.js"></script>