About the <script> tag
Use the <script> tag to implement JavaScript in a web page.
Set the type attribute to “text/javascript”
<script type=”text/javascript”></script>
Two ways to use the <script> tag
- Container: the script tag contains JavaScript
- External source: the script tag refers to an external file containing JavaScript
Script container
JavaScript code goes inside the <script> opening tag and the </script> closing tag.
Script source
Set the <script type=”text/javascript”> tag’s src (Source) property to a JavaScript file
<script type=”text/javascript” src=”somefile.js”></script>
JavaScript files
- JavaScript files have the .js file extension.
- JavaScript files contain only JavaScript.
- JavaScript files do not contain the <script> tag; web pages contain <script> tags.
Where does the <script> tag go?
- Must be inside <head> or <body> tag
- Commonly found in <head> tag, or immediately before </body> tag
- When using document.write method, <script> tag must appear in where want to write content
— Avoid using document.write