Where to insert Javascript Flashcards
The Tag In HTML, JavaScript code must be inserted between and tags.
The Script> Tag
In HTML, JavaScript code must be inserted between and tags.
h2>JavaScript in Body /h2>
script>
document.getElementById(“demo”).innerHTML = “My First JavaScript”; /script>
JavaScript in head>
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:
script> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } /script> /head> body>
h2>JavaScript in Head/h2>
p id=”demo”>A Paragraph./p>
button type=”button” onclick=”myFunction()”>Try it/button>
JavaScript in body>
In this example, a JavaScript function is placed in the body> section of an HTML page.
The function is invoked (called) when a button is clicked:
button type=”button” onclick=”myFunction()”>Try it/button>
script> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } /script>
Placing scripts at the bottom of the body> element improves the display speed, because script interpretation slows down the display.
External JavaScript
Scripts can also be placed in external files:
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 script> tag:
script src=”myScript.js”>/script>
script src=”/js/myScript1.js”>/script>
script src=”myScript1.js”>/script>