Where to use JavaScript, Internal JavaScript, External JavaScript
Where to put JavaScript..?
JavaScript can be placed between <head> or <body> or both tags in HTML pages.
Enter the JavaScript code in the <script> tag.
If you want to use JavaScript code in HTML, you must write the JavaScript code between the <script> and </script> tags.
Example:
- <script>
- document.getElementById("test").innerHTML = "My First JavaScript";
- </script>
JavaScript in <head> or <body>
You can write / put as many JavaScript code as you want in the HTML document.
JavaScript can be placed in HTML <body> or <head> or both.
The best practice is to keep all the code in one place.
<head> Element JavaScript
In this example, the JavaScript function is placed in the <head> section of the HTML page.
If you click the button, the function will respond to your call
Example:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <script>
- function myFunc() {
- document.getElementById("test").innerHTML = "The paragraph has changed ";
- }
- </script>
- </head>
- <body>
- <h1>The head section is written in JavaScript code</h1>
- <p id="test">General paragraph</p>
- <button type="button" onclick="myFunc()">Let's try</button>
- </body>
- </html>
Output:
JavaScript in <body>
In this example the JavaScript function is placed in the <body> element of the HTML page.
If you click the button, the function will respond to your call
Example:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- </head>
- <body>
- <h1>The bodysection is written in JavaScript code</h1>
- <p id="test">General paragraph</p>
- <button type="button" onclick="myFunc()">Let's try</button>
- <script>
- function myFunc() {
- document.getElementById("test").innerHTML = "The paragraph has changed ";
- }
- </script>
- </body>
- </html>
Output:
It is best to keep JavaScript at the bottom of the <body> element.
This makes your page load faster. This is because the JavaScript compiler can slow down your page.
External JavaScript:
JavaScript key can be placed in an external file
script.js
- function myFunc() {
- document.getElementById("test").innerHTML = "The paragraph has changed ";
- }
External JavaScript must be used when the same type of script code needs to be used on multiple web pages.
The file extension of the JavaScript file is .js.
To use external JavaScript, place the JavaScript file in the src attribute of the <script> tag:
Example:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- </head>
- <body>
- <h1>This is External JavaScript</h1>
- <p id="test">General paragraph</p>
- <button type="button" onclick="myFunc()">Let's try</button>
- <script src="Script.js"></script>
- </body>
- </html>
Output:
You can place external JavaScript references / files in <head> or <body> of your choice.
Reference codes behave as if they were placed in a <script> tag.
The <script> tag cannot be used in external scripts.
Advantages of External JavaScript:
There are some special benefits to placing JavaScript in an external file:
It separates HTML and JavaScript code.
HTML and JavaScript code are more readable and can be easily maintained.
Cached JavaScript file speeds up page loading.
External Js, internal javascript, internal js, javascript installations, js installations, js basic, javascript basic, External javascript
shelleyakter
plz help me