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 target Attribute
Photo Credit to CodeToFun
🙋 Introduction
In HTML, the target
attribute is a powerful tool used with hyperlinks to specify where the linked document should be displayed.
By default, when a user clicks on a link, the new document opens in the same browser window or tab.
However, the target
attribute allows developers to control this behavior and determine whether the link should open in a new window, a new tab, or within a specific frame.
🎯 Purpose of target
The primary purpose of the target
attribute is to define the browsing context for the linked document.
This attribute provides flexibility in how users interact with linked content, allowing developers to tailor the user experience according to their application's needs.
💎 Values
The target
attribute accepts several values, each influencing where the linked document will be displayed. The commonly used values are:
- _blank: Opens the linked document in a new, unnamed browser window or tab.
_blank.htmlCopied
<a href="https://www.example.com" target="_blank">Visit Example.com</a>
- _self (Default): Opens the linked document in the same browsing context (same window or tab).
_self.htmlCopied
<a href="https://www.example.com" target="_self">Visit Example.com</a>
- _parent: Opens the linked document in the parent frame, if the current document is inside a frame.
_parent.htmlCopied
<a href="https://www.example.com" target="_parent">Visit Example.com</a>
- _top: Opens the linked document in the full, original window, breaking out of any frames.
_top.htmlCopied
<a href="https://www.example.com" target="_top">Visit Example.com</a>
- framename: Specifies the name of the frame where the linked document should be displayed.
framename.htmlCopied
<a href="https://www.example.com" target="myFrame">Visit Example.com</a>
📄 Example
Let's explore a simple example of how to use the target
attribute in an HTML hyperlink:
<a href="https://www.example.com" target="_blank">Visit Example.com</a>
🧠 How it Works
In this example, the link to https://www.example.com will open in a new, unnamed browser window or tab due to the target="_blank" attribute.
🔄 Dynamic Values with JavaScript
The target
attribute can also be dynamically set using JavaScript.
This is useful when you need to change the target based on certain conditions or user interactions. Here's a basic example:
<script>
// Dynamically set target for a hyperlink
document.getElementById("dynamicLink").target = "_parent";
</script>
🧠 How it Works
In this script, the target
attribute is set to _parent for a hyperlink with the id dynamicLink. This would cause the linked document to open in the parent frame if the current document is inside a frame.
🏆 Best Practices
- Consider user experience when choosing the
target
attribute value. Opening links in new windows or tabs may affect the flow of navigation, so use it judiciously. - Be aware that some users may have pop-up blockers enabled, which can interfere with links that attempt to open in new windows.
- Test your links across various browsers to ensure consistent behavior.
🎉 Conclusion
The target
attribute is a valuable tool for controlling how linked documents are displayed in HTML.
By understanding and utilizing this attribute effectively, developers can provide a more tailored and user-friendly browsing experience.
👨💻 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 target Attribute), please comment here. I will help you immediately.