CSS

HTML 5 Notes For Professional – Chapter – 13: 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).
chrsetSpecifies the character encoding used in an external script file, e.g. UTF
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

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

Recent Posts

Unlocking the Secrets of JSON.stringify(): More Than Meets the Eye

JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used in web development. At…

3 months ago

How to Handle AJAX GET/POST Requests in WordPress

AJAX (Asynchronous JavaScript and XML) is a powerful technique used in modern web development that…

4 months ago

Page Speed Optimization: Post-Optimization Dos and Don’ts

Introduction After successfully optimizing your website for speed, it's essential to maintain and build upon…

4 months ago

Ultimate Guide to Securing WordPress Folders: Protect Your Site from Unauthorized Access

Securing your WordPress folders is crucial to safeguarding your website from unauthorized access and potential…

5 months ago

HTML CSS PHP File Upload With Circle Progress Bar

Creating a file upload feature with a circular progress bar involves multiple steps. You'll need…

5 months ago

Using WP Rocket with AWS CloudFront CDN

Integrating WP Rocket with AWS CloudFront CDN helps to optimize and deliver your website content…

5 months ago