The HTML <script>
element is an element that allows you to run JavaScript on your HTML document.
We commonly use <script> element to run JavaScript for dynamically changing the content, add animation to the page and form validation.
How To Add HTML Scripts
To add the <script>
element, it can be placing in either <head>
or <body>
element. If you want to load the script before the content loaded, then place in <head>
. If you want to run the JavaScript when the document is loaded, then place at the end of the <body>
element. It’s all depending when you want to load it first.
To load external script, you can use src
attribute. When loading the external script, please make sure you link the script that’s exactly where the script file located at. Otherwise, it may not works.
<script src="scripts.js"></script>
You can also load the script internally.
<script>
alert("Hello. From Jorcus");
</script>
Useful Attributes
src
– Link to external resourcestype
– Type of scriptasync
– Script will execute synchronously as soon as the script is loaded.defer
– Script will only be executed only after the document is parsed.crossorigin
Default CSS Settings
The <script>
element are set as display:none;
by default. It wouldn’t display directly to the visitors.
script {
display: none;
}