HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- 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 Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML nohref Attribute
Photo Credit to CodeToFun
🙋 Introduction
The nohref
attribute in HTML is used to specify that a particular <area> element in an image map should not be a hyperlink.
This attribute is particularly useful when you want to define areas within an image map that do not lead to any link but still need to be part of the map for various purposes such as visual segmentation or scripting.
🎯 Purpose of nohref
The primary purpose of the nohref
attribute is to prevent certain areas of an image map from being clickable hyperlinks. This can enhance user experience by ensuring that only the intended areas of an image map are interactive, thereby avoiding confusion and improving navigation.
💎 Values
The nohref
attribute is a boolean attribute. When present, it effectively prevents the <area> element from being a link. Since it's a boolean attribute, it does not require a value; its mere presence is sufficient to activate its functionality.
📄 Example:
Here is a basic example of how to use the nohref
attribute within an image map:
<img src="example.jpg" usemap="#imagemap">
<map name="imagemap">
<area shape="rect" coords="34,44,270,350" href="link1.html" alt="Link 1">
<area shape="rect" coords="290,172,333,250" nohref alt="No Link">
</map>
🧠 How it Works
In this example, the second <area> element includes the nohref
attribute, indicating that this area should not be a hyperlink.
🔄 Dynamic Values with JavaScript
Although the nohref
attribute itself is straightforward and does not accept dynamic values, you can manipulate the href attribute of an <area> element using JavaScript to achieve similar functionality. For instance, you can dynamically add or remove the href attribute based on certain conditions.
Here is an example of how you can dynamically change an <area> element to act as if it has the nohref
attribute using JavaScript:
<img src="example.jpg" usemap="#dynamicmap">
<map name="dynamicmap">
<area id="dynamicArea" shape="rect" coords="34,44,270,350" href="link2.html" alt="Dynamic Link">
</map>
<script>
// Function to remove the href attribute dynamically
function removeHref() {
document.getElementById('dynamicArea').removeAttribute('href');
}
// Function to add the href attribute dynamically
function addHref() {
document.getElementById('dynamicArea').setAttribute('href', 'link2.html');
}
// Example usage
removeHref(); // This will make the area act as if it has nohref
// addHref(); // This will restore the href attribute
</script>
🧠 How it Works
In this script, calling removeHref() will remove the href attribute from the <area> element, making it act as though it has the nohref
attribute. Conversely, calling addHref() will add the href attribute back.
🏆 Best Practices
- Use the
nohref
attribute to clearly define non-interactive areas in your image maps, enhancing the overall user experience. - When manipulating href attributes dynamically with JavaScript, ensure that your code handles all possible states to avoid broken links or unexpected behavior.
- Always test your image maps and script modifications across different browsers to ensure consistent functionality.
🎉 Conclusion
The nohref
attribute is a simple yet powerful tool for controlling the interactivity of image maps in HTML.
By understanding its use and leveraging JavaScript for dynamic behaviors, you can create more intuitive and user-friendly web applications.
👨💻 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 nohref Attribute), please comment here. I will help you immediately.