HTML Basics

HTML Introduction HTML Basic HTML Comments HTML Tags/Elements HTML Attributes HTML Formatting HTML Block and Inline HTML Charsets HTML Classes HTML Colors HTML Div HTML Headings HTML Id HTML Iframes HTML Images HTML File Paths HTML Tables HTML Layout HTML Lists HTML Links <a> HTML Paragraphs HTML Quotations HTML JavaScript HTML Emojis HTML URL Encode HTML Entities HTML Computercode HTML Symbols HTML Styles

HTML Forms

HTML Forms HTML Form Elements HTML Form Attributes HTML Input Attributes HTML Input Regex HTML Input Form Attributes HTML Input Types

HTML SEO

HTML Head HTML Page Title HTML Responsive HTML Semantics HTML Favicon

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Audio HTML Video

HTML Reference

a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl DOCTYPE dt em embed fieldset figcaption figure font footer form frame frameset h1_-_h6 head header hgroup hr html i iframe img input ins kbd label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script search section select small source span strike strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr track tt u ul var video wbr

How to use the <script> tag in HTML

The <script> tag is used to define a client-side script, such as a JavaScript. It can be used to execute JavaScript code within an HTML document.

To include JavaScript code in an HTML document, you can either write the code directly within the <script> tag or link to an external script file. Here's an example of both methods:

Example of inline script:

<script>
    document.getElementById("demo").innerHTML = "Hello, World!";
</script>

Example of linking to an external script file:

<script src="myscript.js"></script>

In the first example, the JavaScript code is written directly within the <script> tag. In the second example, the script is linked to an external file called myscript.js.

Main attributes of the <script> tag:

Attribute Description
src Specifies the URL of an external script file.
type Specifies the MIME type of the script. Default value is "text/javascript".
async Specifies that the script is executed asynchronously (only for external scripts).
defer Specifies that the script is executed after the document has been parsed (only for external scripts).

Here's an example of an external script file with the src attribute:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

In this example, the jQuery library is loaded from an external URL using the src attribute.

Remember to always include the closing tag </script> at the end of the script code.