The Developer Brains


Include JavaScript Code in HTML

Web Designing | Web Development


SEE MORE














</Include JavaScript Code in HTML>





Include JavaScript Code in HTML

In HTML ,We can right javacript 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>
				
				


SEE MORE




















</Include JavaScript Code in HTML. >


Include JavaScript Code in HTML.

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

Handling disabled Javascript

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>

				
				
Try it Yourself

Linking to an external JavaScript file

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>
				
				
Try it Yourself

Directly including JavaScript code

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>

				
				
Try it Yourself

Including a JavaScript file executing Synchronous , asynchronously and Deferred

Synchronous

				
<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.

Asynchronous

				
<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.

Deferred

				
<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

  • HTML parsing
  • HTML parsing paused
  • script download
  • script execution

<script>

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.


<script async>

async download the file during HTML parsing and will pause the HTML parser to excute it when it has finished downloading


<script defer>

defer download the file during HTML parsing and will excute it after the parser has completed.defer script are also guarenteed to execute in the order that they appear in the document














Next: Using HTML with CSS


Previous

      

Next Page













  Share TDB SCHOOL  

Share this E-Learning Website on social media platforms with your friends and followers