Script tag in HTML
- 24-07-2022
- Trung Minh
- 0 Comments
In this article we will learn the script tag in HTML, this is the tag used to declare Javascript code for the web page.
1. What is a script tag?
The script tag defines the Javascript code within it. The script can contain Javascript statements or link to an external Javascript file.
If the html document does not use javascript then you do not need to declare the script tag.
This tag can be placed anywhere on the website. However, usually people will put it in the head
tag or at the bottom of the page.
It has two basic uses as follows:
<script> // Mã JS </script> <script src="URL_JS_FILE"></script>
In which way 1 is that we will put the JS code inside. As for the second way, we will put it in a separate .js
file, then pass it to the src
attribute of the tag.
2. How to use script tags in HTML
Example: Using script tag to attach button click event:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> function myFunction() { document.getElementById("demo").innerHTML = "Hello web888.vn!"; } </script> </head> <body> <h1>Học lập trình miễn phí tại web888.vn</h1> <p>Click để chạy đoạn scipt!</p> <button onclick="myFunction()">Run script</button> <p id="demo"></p> </body> </html>
If you have not learned javascript, you can temporarily skip this part.
Attributes of the script tag:
- charset – specifies the character encoding to be used.
- src – the path to the javascript file you want to link.
Browser support:
- Chrome
- Firefox
- IE
- Safari
- Opera