Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a target attribute in HTML

Understanding the Target Attribute

Before we dive into the specifics of adding a target attribute in HTML, it's helpful to understand what we mean by 'attribute'. In the simplest terms, an attribute is a way of defining properties or characteristics for an HTML element. It's like attaching a sticky note to an object, giving it a certain characteristic. For HTML, the target attribute is one of these sticky notes.

The target attribute is primarily used within a (anchor) and form tags. It defines where the linked resource will open or where the submitted form data will be displayed. Now, let's dive into the specifics of how to add a target attribute in HTML.

Using the Target Attribute in Anchor Tags

Anchor tags (a tags) are used to create hyperlinks in HTML. The target attribute is used to specify where the link should open. There are four possible values for the target attribute:

  • _blank: Opens the link in a new window or tab
  • _self: Opens the link in the same frame (this is the default behavior)
  • _parent: Opens the link in the parent frame
  • _top: Opens the link in the full body of the current window

Let's look at an example of how to use the target attribute in an a tag:

<a href="https://www.example.com" target="_blank">Visit Example.com</a>

In this example, clicking on the hyperlink text "Visit Example.com" will open the link "https://www.example.com" in a new window or tab.

Using the Target Attribute in Form Tags

The target attribute can also be used in form tags to specify where the server's response will be displayed after the form data has been submitted. The possible values are the same as with a tags. Here's an example:

<form action="/submit_form" method="post" target="_blank">
    <!-- form elements go here -->
</form>

In this example, when the form is submitted, the server's response will be displayed in a new window or tab.

Why and When to Use the Target Attribute

The target attribute is a useful tool that can enhance the user experience on a webpage by controlling the behavior of links and form submissions. For instance, using target="_blank" can be handy when you want to keep a user on your page while also providing them with additional information or resources. However, it's worth noting that excessive use of target="_blank" can lead to a cluttered browsing experience with too many tabs or windows.

A Word of Caution with Target="_blank"

While target="_blank" can be useful, it can also pose a security risk. By default, the new page has access to the window.opener property, which means it can potentially manipulate the original page. To prevent this, use rel="noopener" alongside target="_blank", like this:

<a href="https://www.example.com" target="_blank" rel="noopener">Visit Example.com</a>

This ensures that the new page cannot access your page via the window.opener property.

Recap and Conclusion

We've now covered what the target attribute is, how to use it in a and form tags, when and why to use it, and even a key security consideration. Remember, it's all about enhancing the user experience. Use the target attribute wisely, and always with the user in mind.

Keep practicing with different target values and see how they change the behavior of your links and forms. Happy coding!