
HTML
HTML provides the <script>
tag in order to put different scripts inside the HTML. The <script> is generally used for JavaScript but can be also used for other scripting languages. The <script> generally contains scripting statements, functions, variables etc.
<script> Tag Attributes
Event the <script> tag is mostly related to the scripts it provides some attributes to set script features. For example async
attribute is used to download the specified script file asynchronously which makes the page load faster.
Attribute | Value | Description |
---|---|---|
async | async | Specifies that the script is downloaded in parallel to parsing the page, and executed as soon as it is available (before parsing completes) (only for external scripts) |
crossorigin | anonymous use-credentials | Sets the mode of the request to an HTTP CORS Request |
defer | defer | Specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing (only for external scripts) |
integrity | filehash | Allows a browser to check the fetched script to ensure that the code is never loaded if the source has been manipulated |
nomodule | True False | Specifies that the script should not be executed in browsers supporting ES2015 modules |
referrerpolicy | no-referrer no-referrer-when-downgrade origin origin-when-cross-origin same-origin strict-origin strict-origin-when-cross-origin unsafe-url | Specifies which referrer information to send when fetching a script |
src | URL | Specifies the URL of an external script file |
type | scripttype | Specifies the media type of the script |
<script> Tag Example
The Javascript scripts can be put inside the <script> tags. The <script> tag is used to set the start of the script. The </script> tag is used to express the end of the script. All content between <script> and </script> are interpreted as script and treated different than HTML tags. The type
attribute is used to specify the script type. In the following example we set the type attribute as text/JavaScript
.
<html>
<head>
<title>Script Tag Example</title>
</head>
<body>
<script type = "text/JavaScript">
document.write("You're visiting wisetut!")
</script>
</body>
</html>
Download Script As File
Another usage for the <script> tag is specifying the script file location by using the src
attribute. In the following example we load the script from the www.wisetut.com .
<html>
<head>
<title>Script Tag Example</title>
</head>
<body>
<script type = "text/JavaScript" src="https://www.wisetut.com/main.js">
</body>
</html>