In HTML ,We can right JavaScript code in between script tags or we can also link external JavaScript .
example:-
<script>
document.write("Hello, world!");
</script>
<!--or -->
<script src="path/to.js"></script>
Attribute | Details |
---|---|
src | Specifies the path to a JavaScript file. Either a relative or absolute URL. |
type | Specifies the MIME type. This attribute is required in HTML4, but optional in HTML5. |
async | Specifies that the script shall be executed asynchronously (only for external scripts). This attribute does not require any value (except of XHTML). |
defer | Specifies that the script shall be executed when the page has finished parsing (only for external scripts). This attribute does not require any value (except of XHTML) |
charset | Specifies the character encoding used in an external script file, e.g. UTF-8 |
crossorigin | How the element handles crossorigin requests |
nonce | Cryptographic nonce used in Content Security Policy checks CSP3 |
It is possible that the client browser does not support JavaScript or have JavaScript execution disabled, perhaps due to security reasons. To be able to tell users that a script is supposed to execute in the page, the <noscript> tag can be used. The content of <noscript> is displayed whenever JavaScript is disabled for the current page.
<script>
document.write("Hello, world!");
</script>
<noscript>This browser does not support Javascript.</noscript>
The src attribute works like the href attribute on anchors: you can either specify an absolute or relative URL. The example above links to a file inside the same directory of the HTML document. This is typically added inside the <head>tags at the top of the html document
<script src="example.js"></script>
Instead of linking to an external file, you can also include the JS code as-is in your HTML:
<script>
// JavaScript code
alert("hello")
</script>
<script src="path/to.js"></script>
Standard practice is to place JavaScript <script> tags just before the closing </body> tag. Loading your scripts last allows your site's visuals to show up more quickly and discourages your JavaScript from trying to interact with elements that haven't loaded yet.
<script src="path/to.js" async></script>
Another alternative, when the JavaScript code being loaded is not necessary for page initialization, it can be loaded asynchronously, speeding up the page load. Using async the browser will load the contents of the script in parallel and, once it is fully downloaded, will interrupt the HTML parsing in order to parse the Javascript file.
<script src="path/to.js" defer></script>
Deferred scripts are like async scripts, with the exception that the parsing will only be performed once the HTML is fully parsed. Deferred scripts are guaranteed to be loaded in the order of declaration, same way as synchronous scripts.
Example to demonstrate Synchronous , asynchronously and Deferred
Let's start by defining what <start> without any attribute does. The HTML file will be parsed until the script file is hit, at that point parsing will stop and a request will be made to fetch the file (if it's external).The script will then be executed before parsing is resumed.
async download the file during HTML parsing and will pause the HTML parser to execute it when it has finished downloading
defer download the file during HTML parsing and will execute it after the parser has completed. defer script are also guarantied to execute in the order that they appear in the document