Include JavaScript Code in HTML

Include JavaScript Code in HTML
Nikhil
Published on 2021-07-01 16:12:37

Include JavaScript Code in HTML using javascript tags and Handling Javascript

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 for 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 in HTML


It is possible that the client browser does not support Javascript or has Javascript execution disabled, perhaps due to security reasons. To be able to tell users that a script is supposed to execute on 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


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

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.

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
</script>

Including a JavaScript file executing asynchronously


<script type="text/javascript" src="URL" async></script>

ATutorialHub Related Guide

HTML Tutorials Comments (8)

Leave a Comment

Your email address will not be published. Required fields are marked*

User Comments

html tutorial comments

panduranga gupta

2021-07-05 07:03:13

good website for learning and help me a lot

html tutorial comments

raju

2021-09-25 14:58:47

The awsome website i am looking like for a long time, good work atutorialhub team keep doing

html tutorial comments

Shivani

2021-09-01 15:03:56

Learning a lot from the courses present on atutorialhub. The courses are very well explained. Great experience

html tutorial comments

Harshitha

2021-09-10 15:05:45

It is very helpful to students and easy to learn the concepts

html tutorial comments

Sowmya

2021-09-14 15:06:41

Great job Tutorials are easy to understand Please make use of it

html tutorial comments

Zain Khan

2021-09-18 15:07:23

Great content and customized courses.

html tutorial comments

Rudrakshi Bhatt

2021-09-09 15:08:10

Well structured coursed and explained really well!

html tutorial comments

Pavana Somashekar

2021-09-11 15:09:08

Good platform for beginners and learn a lot on this website