Where to add script tag in HTML
What you will learn here about where to add script tag in HTML.
There are 3 ways to add JavaScript in HTML code and those are
- JavaScript in head
- JavaScript in body
- external JavaScript in html
1)JavaScript in head
In the head section of HTML code, JavaScript is added in <script> tag.
<head>
<script>
var name=”Jon Dow”;
console.log(name);
</script>
</head>
2)JavaScript in body
In the body section of HTML code, JavaScript is also added in <script> tag. This <script> tag we can place anywhere in the body. Generally we keep <script> tag before closing the <body> tag.
<body>
<script>
document.getElementById(‘demo’).innerHTML=””Javascript Example”;”;
</script>
</body>
3)External JavaScript in HTML
External JavaScript in HTML code is added using <script> tag with src attribute. The value of src attribute is the URL of external javascript file.
<body>
<script src=”https://www.bytesofgigabytes.com/codes/script.js” >
</script>
</body>