HTML <script> tag

HTML <script> tag

The <script> tag is used to add JavaScript code in an HTML document.

The <script> tag can be used to add either internal or external JavaScript code.

Internal JavaScript Code

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Click Me</button> <script> function myFunction(){ alert("I'm from Internal javascript file"); } </script> </body> </html>

External JavaScript Code

The external JavaScript file file used in this example is alert-me.js.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Click Me</button> <script src="/script/alert-me.js"></script> </body> </html>

Attributes

The following are the attributes of <script> tag. Also try Global attributes.

AttributeValueExplanation
async-Specifies that external javascript will execute only after parsing the entire page.
Order of execution is no guarantee.
defer-Specifies that external javascript will execute only after parsing the entire page.
Order of execution is guarantee.
nomodule-Specifies the script will run only when browser do not support ES2015 modules.
nonce-Specifies the inline content of a script was not injected into the document by third party to cause malicious to the website, but written intentionally by the author who holds the rights to access the server of the website.
srcURLSpecifies the URL of the external javascript.
typemedia_typeSpecifies the media type of the script.

Deprecated Attributes

The following attribute should not be used in any case.

AttributeValueExplanation
charsetcharsetSpecifies the character encoding used in an external script file.
languagenameSpecifies the scripting language in use.

Reminder

Hi Developers, we almost covered 99.5% of HTML Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in HTML.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Meet the Author