HTML - target Attribute



The target is an HTML attribute that is used to specify where to open the linked document. It is used with the anchor <a>, <area>, <base>, and <form> tags.

The target attribute specifies the default target for all hyperlinks and forms in the page for the <base> tag. And this attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form for the <form> tag.

Syntax

Following is the syntax of the target attribute −

<element target = "_blank | _self | _parent | _top | framename " \>

Following are the attribute values –

  • _blank − it opens the link in a new window.
  • _self − it opens the link in a same frame it is a default value.
  • _parent − it opens the link in the parent frameset.
  • _top − it opens the linked document in the full body of the window.
  • frameset − it opens the linked document in the named frame.

Example

In the following example, let’s see the usage of target attribute along with value "_blank".

<!DOCTYPE html>
<html>
<body>
   <h3>tutorialspoint</h3>
   <h2>target attribute</h2>
   <p>Open link in a new window or tab: <a href="https://www.tutorialspoint.com/index.htm" target="_blank">Visit tutorialspoint</a>
   </p>
</body>
</html>

On running the above code, the output window will pop up displaying the hyperlink on the webpage. when the user clicks on the hyperlink it will opens in new tab.

Example

Considering the another scenario, where we are going to use the target attribute with the value '_self'.

<!DOCTYPE html>
<html>
<body>
   <h3>tutorialspoint</h3>
   <h2>target attribute</h2>
   <p>Open link in a same window or tab: <a href="https://www.tutorialspoint.com/index.htm" target="_self">Visit tutorialspoint</a>
   </p>
</body>
</html>

When we run the above code, it will generate an output consisting of the hyperlink displayed on the webpage. when the user clicks on the link it will opens in the same tab itself.

Example

Let's look into the following example, where we are going to use the target attribute with the value '_parent'.

<!DOCTYPE html>
<html>
<body>
   <h3>tutorialspoint</h3>
   <h2>target attribute</h2>
   <p>Open link in a parent tab: <a href="https://www.tutorialspoint.com/index.htm" target="_parent">Visit tutorialspoint</a>
   </p>
</body>
</html>

On running the above code, the output window will pop up displaying the hyperlink on the webpage. when the user clicks on the link it will opens in the same frameset.

Example

Following is the example, where we are using the <base> tag with the target attribute, and the value of the target is "_self"

<!DOCTYPE html>
<html>
<head>
   <base target="_self">
   <title> HTML Base target Attribute </title>
</head>
<body style="text-align:center;">
   <h1 style="color:green;">tutorials <span style="color: black">point</span>
   </h1>
   <h2>HTML Base target Attribute</h2>
   <a href="https://www.tutorialspoint.com/index.htm" alt="GFG"> visit tutorialspoint </a>
</body>
</html>

When we run the above code, it will generate an output consisting of the text along with a hyperlink displayed on the webpage. when the user clicks on the link it will open in the same tab or window.

html_attributes_reference.htm
Advertisements