FOLLOW US ON TWITTER



FOLLOW US



img/blogimg/html5.jpg


@harish 574

Include JavaScript Code in HTML


2021-11-17 16:02 · 7 min read

Include JavaScript Code in HTML

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>

Include JavaScript Code in HTML.

AttributeDetails
srcSpecifies the path to a JavaScript file. Either a relative or absolute URL.
typeSpecifies the MIME type. This attribute is required in HTML4, but optional in HTML5.
asyncSpecifies that the script shall be executed asynchronously (only for external scripts). This attribute does not require any value (except of XHTML).
deferSpecifies 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)
charsetSpecifies the character encoding used in an external script file, e.g. UTF-8
crossoriginHow the element handles crossorigin requests
nonceCryptographic 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>

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> 

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>

 

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 execute it when it has finished downloading

<script defer>

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

 

TAGS:






POST COMMENTS
No Comment

Please login to post comment




POPULAR POSTS

best free web hosting services...
what is new in miui 11?...
free website ssl security using cloudfla...
do you want to make money online?...
CSS3 @IMPORT RULES...
What is HTML Anchor Tag and Hyperlinks?...
CSS3 RESPONSIVE UI...
How to host a web page in 10 minutes....
Tables HTML...
CSS3 FUNCTIONS...
How to use CSS3 Overflow Property?...
CSS3 FILTERS...


RECENT COMMENTS

2022-03-05 11:05
By coder on XAMPP - MySQL shutdown unexpectedly | How to fix MySQL crash unexpectedly
2021-10-12 12:34
By abnongoke on do you want to make money online?
2021-10-12 12:34
By abnongoke on how to get a free website domain name?
2021-10-12 12:34
By solar panel for shed on what is new in miui 11?
2021-10-12 12:34
By solar panel for shed on best free web hosting services
2021-10-12 12:34
By amos tanui on free website ssl security using cloudflare


SOCIAL SHARE



FOLLOW US ON TWITTER



FOLLOW US ON FACEBOOK



FOLLOW US







Recent Blogs



Contact Us

Subscribe Our News-Letter



Kashipur, 244713
Uttarakhand, India



© blog.theprodevelopers.com 2024. All Rights Reserved. Powered by Pro Developer