Categories
HTML Web development

HTML script and noscript tags

HTML script tag and noscript tag

The HTML <script> tag is used in the HTML document for using the JavaScript in the code. The HTML <noscript> tag is used to display the alternate text on the browser that does not support scripts.

The HTML <script> tag like also <noscript> tag can be used in both <head> and <body> sections.

HTML <script> tag

The HTML <script> element is used to embed a client-side script (JavaScript).

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>
<script>
document.getElementById("example").innerHTML = "Hello World!";
</script> 

</body>
</html>

Output:

Attributes:

async (value- async) – defines that the script is executed asynchronously (can be only used if the src attribute is present).

Syntax:

<script async>

crossorigin (value- anonymous/ use-credentials) – sets the mode of the request to an HTTP CORS Request.

Syntax:

<script crossorigin="anonymous|use-credentials"> 

defer (value- defer) – defines that the script is executed when the page has finished parsing (can be only used if the src attribute is present).

Syntax:

 <script defer> 

integrity (value- filehash) – allows a browser to check the fetched script to ensure that the code is never loaded if the source has been manipulated.

Syntax:

<script integrity="filehash"> 

nomodule (value- true/false) – defines that the script should not be executed in browsers that support ES2015 modules.

referrerpolicy (value- no-referrer/ no-referrer-when-downgrade/ origin/ origin-when-cross-origin/ same-origin/ strict-origin/ strict-origin-when-cross-origin/ unsafe-url) – defines which referrer information to send when fetching a script.

Syntax:

<script referrerpolicy="no-referrer|no-referrer-when-downgrade|origin|origin-when-cross-origin|same-origin|strict-origin-when-cross-origin|unsafe-url"> 

src (value- URL) – defines an URL of an external script file.

Syntax:

<script src="URL"> 

type (value- scripttype) – defines the media type of the script.

Syntax:

<script type="scripttype"> 

HTML <noscript> tag

The HTML <noscript> element specifies different content to be displayed to users that have disabled scripts in their browser/ or have a browser that doesn’t support script.

Example:

<!DOCTYPE html>
<html>
<body>

<script>
document.write("Hello World!")
</script>
<noscript>Sorry, your browser doesn't support JavaScript!</noscript>

</body>
</html>

Output:

Enjoy coding!