Signup/Sign In
PUBLISHED ON: JULY 17, 2021

How To Open link in the current window in HTML?

When you click on any link it can be opened either on the same tab or some new tab. Users may not like opening the new tab every time and it may annoy them. We can open the link in the same window using HTML attributes.

The Target attribute

The target attribute is used to control where the link opens when you click on them. To open the link in the same window, use target="-self" within <a> element. By default, the link opens in the same window.

Example: Without using the target attribute

Here in the example, we have not used the target attribute to the links.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML</title>

</head>
<body>
    <h2>Open link by default</h2>
    <a href="https://www.studytonight.com/" > Visit page </a>
</body>
</html>

Output

Here is the output of the above program. The link opens in the same window when clicked.

Default click

Example: Using target="_self" attribute

Here, we have used the target="_self" attribute within <a> element. It directs the link to open on the same page.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML</title>

</head>
<body>
    <h2>Open link by target attribute</h2>
    <a href="https://www.studytonight.com/" target="_self" > Visit page </a>
</body>
</html>

Output

Here is the output of the above program where we can see that the link opens in the same tab.

using target attribute

Conclusion

For opening the link in the same window we can use the target="_self" attribute. By default, the links open in the same window. But if some browser does not support it, we can use the target attribute.



About the author: