HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML nomodule Attribute
Photo Credit to CodeToFun
🙋 Introduction
The nomodule
attribute is a feature in HTML that allows developers to specify that a script should only be executed in browsers that do not support JavaScript modules.
This attribute is particularly useful for providing fallback scripts for older browsers that do not support the modern ES6 module system.
🎯 Purpose of nomodule
The primary purpose of the nomodule
attribute is to ensure compatibility with older browsers while allowing the use of modern JavaScript modules. By using this attribute, you can include both modern and legacy scripts in your web pages, ensuring that all users, regardless of their browser's capabilities, have a functional experience.
💎 Values
The nomodule
attribute is a boolean attribute, which means it does not require a value. Its presence alone is enough to specify that the script should not be executed in module-supporting browsers.
📄 Implementation Example:
Let's look at a simple example of how to use the nomodule
attribute in an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML nomodule Attribute Example</title>
</head>
<body>
<!-- Modern JavaScript module -->
<script type="module">
import { modernFunction } from './modern-module.js';
modernFunction();
</script>
<!-- Legacy JavaScript script for older browsers -->
<script nomodule src="legacy-script.js"></script>
</body>
</html>
🧠 How it Works
In this example, the browser will load and execute modern-module.js if it supports JavaScript modules. If it does not support modules, it will fall back to executing legacy-script.js.
🔄 Dynamic Values with JavaScript
You can also dynamically set the nomodule
attribute using JavaScript. This can be useful if you need to add scripts conditionally based on runtime conditions. Here's a brief example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic nomodule Example</title>
</head>
<body>
<script>
// Create a script element for a legacy script
var script = document.createElement('script');
script.src = 'dynamic-legacy-script.js';
script.setAttribute('nomodule', '');
// Append the script to the body if needed
if (!('noModule' in HTMLScriptElement.prototype)) {
document.body.appendChild(script);
}
</script>
</body>
</html>
🧠 How it Works
In this script, a script element with the nomodule
attribute is created dynamically. The script is appended to the body only if the browser does not support the noModule property, ensuring that it runs only in legacy environments.
🏆 Best Practices
- Use the
nomodule
attribute to maintain compatibility with older browsers while leveraging modern JavaScript modules for browsers that support them. - Ensure that the scripts specified with nomodule provide necessary functionality for legacy browsers without duplicating logic present in modern modules.
- Test your web application across different browsers to ensure that both module and nomodule scripts execute as expected.
🎉 Conclusion
The nomodule
attribute is a powerful tool for managing script compatibility across different browser environments.
By understanding and using this attribute appropriately, you can ensure that your web applications provide a seamless experience for all users, regardless of their browser capabilities.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML nomodule Attribute), please comment here. I will help you immediately.