Loading Please Wait...
We can place JavaScript code in many places within HTML or link external files that is internal JavaScript and External JavaScript
Internal JavaScript included within the same HTML file.
Internal JavaScript included using <script> and </script> tags.
Internal JavaScript can be placed inside <head> element or <body> element.
JavaScript can be placed inside <head> element.
<!DOCTYPE html>
<html>
<head>
<script>
var clickTime = 0;
function myFunction() {
document.getElementById("demo").innerHTML = "Button clicked " + (clickTime++) + " times.";
}
</script>
</head>
<body>
<h1>JS Demo</h1>
<p id="demo">Button clicked 0 times.</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
JavaScript can be placed inside <body> element.
<!DOCTYPE html>
<html>
<body>
<h1>JS Demo</h1>
<p id="demo">Button clicked 0 times.</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
var clickTime = 0;
function myFunction() {
document.getElementById("demo").innerHTML = "Button clicked " + (clickTime++) + " times.";
}
</script>
</body>
</html>
External JavaScript included using <script> tag pointing to a JavaScript file or resource using src attribute.
JavaScript files must have file extension .js
External JavaScript can be placed inside <head> element or <body> element as well.
var clickTime = 0;
function myFunction() {
document.getElementById("demo").innerHTML = "Button clicked " + (clickTime++) + " times.";
}
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<h1>JS Demo</h1>
<p id="demo">Button clicked 0 times.</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JS Demo</h1>
<p id="demo">Button clicked 0 times.</p>
<button type="button" onclick="myFunction()">Try it</button>
<script src="script.js"></script>
</body>
</html>
It separates HTML and code
It makes HTML and JavaScript easier to read and maintain
Cached JavaScript files can speed up page loads
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved