Html href to open in new window

You can use the HTML code on this page to force a link to open in a new window.

Example

As the above example demonstrates, to open a link in a new window, you simply add target=»_blank» to the anchor tag. Everything else stays as a normal link.

If you have many links, and you add target=»_blank» to each one, they will all open in their own blank window. This could become quite annoying for the user—especially if you have many links that each open in their own tab or window. Users could avoid clicking on links if a blank window opens every time they click.

There is a way to overcome this issue. Read on…

Reloading the Blank Window/Tab

Instead of calling your window _blank , you can give it a name of your choosing. Then all URLs will load in that same tab (as long as you use the same name).

For example, if you have a policy of loading all external links in a separate tab, you could call that tab, say, external . Then each link that contains target=»external» will open in that window/tab. Doing this ensures that new windows aren’t going to be popping up every time a user clicks on a link.

Читайте также:  Css backgrounds and borders

Here’s an example of what happens when you use target=»_blank» for all links:

And here are the same links but with target=»external» (but this could be any name you choose):

There are other keywords that can be used with target as well as various implications depending on the names of windows/tabs that are already open, whether they’re sandboxed, etc. See the W3C website if you’re interested.

Creating a «Popup Window»

You can also use JavaScript to open a new window. Using JavaScript, you can also specify what the window should look like. This is a common technique for creating a «popup window». For more info, see Popup Windows.

You can do much more with HTML links. Here are more HTML link codes — each one catering for a specific purpose.

  • HTML link code
  • HTML image link
  • HTML email links
  • About HTML links (from the HTML tutorial)
  • HTML tag
  • HTML redirect code
  • Timed redirect (using JavaScript)
  • CSS links (remove underline, hover over effect, etc)

Источник

Hyperlinks are used to jump from one page to another. A hyperlink commonly opens in the same page by default. But it is possible to open hyperlinks in a separate window.

Opening external links in a new tab or window will make people leave your website. In this way, you prevent your visitors from returning to your website. Remember that visitors can open a new tab themselves and are irritated when a link opens in a new tab or window without their consent. That’s why it’s recommended to avoid opening links in a new tab or window. However, there can be specific situations when this is needed, and in this snippet, we’ll demonstrate how it can be done.

When it is needed to tell the browser where to open the document, the target attribute is used.

How to Add target=»_blank» Attribute to Anchor Link

The target attribute determines where the linked document will open when the link is clicked. It opens the current window by default. To open a link in a new window, you need to add the target=»_blank» attribute to your anchor link, like the following.

html> html> head> title>Title of the document title> head> body> h1>Hyperlink Example h1> p> This a href="https://www.w3docs.com/" target="_blank">hyperlink a> will open in a new tab. p> body> html>

Result

In the given example, when the visitor clicks on the hyperlink, it opens in a new window or tab.

There is another way of opening a hyperlink in a new tab by using the JavaScript window.open function with the onclick event attribute like this:

html> html> head> title>Title of the document title> style> .link:hover < text-decoration: underline; color: blue; cursor: pointer; > style> head> body> h1>Hyperlink Example with JavaScript h1> p>Visit our website p> a href="https://w3docs.com" onclick="window.open(this.href, '_blank', 'width=500,height=500'); return false;" class="link">W3docs a> body> html>

Let’s see one more example, where besides the target attribute, we also add a rel attribute with the “noopener noreferrer” value. The rel attribute is not mandatory, but it’s recommended as a security measure.

html> html> head> title>Title of the document title> head> body> a target="_blank" rel="noopener noreferrer" href="https://www.lipsum.com/">Lorem Ipsum a> p>This link will open in a new browser tab or a new window. p> body> html>

Источник

That will open a new window, not tab (with JavaScript, but quite laconically):

Depending on the OP’s definition of «without javascript» (this answer uses JS but without including any JS files or

@KyleClegg I think you would add the «top» and «left» attributes to the window.open() call, but it is up to you do decide how large the screen is. I don’t believe there is an automatic way to center a new window. This link may help: w3schools re window open

Also if you don’t want to repeat the link you can use: onclick=»window.open(this.href,’newwindow’,’width=1000,height=800′); return false;» this uses the href of element

With pure HTML you can’t influence this — every modern browser (= the user) has complete control over this behavior because it has been misused a lot in the past.

HTML option

You can open a new window (HTML4) or a new browsing context (HTML5). Browsing context in modern browsers is mostly «new tab» instead of «new window». You have no influence on that, and you can’t «force» modern browsers to open a new window.

In order to do this, use the anchor element’s attribute target [1] . The value you are looking for is _blank [2] .

JavaScript option

Forcing a new window is possible via javascript — see Ievgen’s excellent answer below for a javascript solution.

(!) However, be aware, that opening windows via javascript (if not done in the onclick event from an anchor element) are subject to getting blocked by popup blockers!

[1] This attribute dates back to the times when browsers did not have tabs and using framesets was state of the art. In the meantime, the functionality of this attribute has slightly changed (see MDN Docu)

[2] There are some other values which do not make much sense anymore (because they were designed with framesets in mind) like _parent , _self or _top .

Источник

You should add the target=»_blank» and rel=»noopener noreferrer» in the anchor tag.

Adding rel=»noopener noreferrer» is not mandatory, but it’s a recommended security measure. More information can be found in the links below.

_blank is handled on a vendor basis. Most modern browsers will, by default, open in a new tab. Older browsers, such as the IE hoard, will open in a new window — either because they don’t have a tabbed feature, or because their default behaviour is a new window.

When not using a special name, as specified in links above, target is the name of the window (or «frame») you’re targeting. If you set it to _tab or _new then it opens a window with that name. If a user clicks that link, goes back to your initial page and clicks another link with the same target, the tab/window opened the first time should have the content loaded into it.

It shouldn’t be your call to decide whether the link should open in a new tab or a new window, since ultimately this choice should be done by the settings of the user’s browser. Some people like tabs; some like new windows.

Using _blank will tell the browser to use a new tab/window, depending on the user’s browser configuration and how they click on the link (e.g. middle click, Ctrl +click, or normal click).

Additionally, some browsers don’t have a tabs feature and therefore cannot open a link in a new tab, only in a new window.

I have to agree fully with taking away the decision from the user or browser. The user will have a preference which they are accustomed to and this removes their right to their preference. However, that being said. there are times that this is needed. (print dialogs, etc)

Although I feel this is true in many cases, for less technical users, I think (my opinion) that this simply is frequently not true. Some users may need this help or they risk the chance of not knowing how to get back to a page that they wanted left open. My two cents to try to put a little more of a user experience perspective on things.

I would agree with the «shouldn’t» for websites, but remember that not all HTML is written for websites. I was thinking of something for an in-house web-app where it would be nice make some links open in new tabs without teaching everyone in the office how to ctrl+click.

I would patently disagree that this is a «best practice», and argue that it depends entirely on the use case. For instance, I work on a web application where the user is performing most of their daily work, and having a link navigate in the same tab would be terrible. Likewise, if I have a tutorial/instructional page, I would link out to related materials, and wouldn’t want to navigate in-place. However, if the page is just intended to be consumed by the user, navigating in-place would likely be more ideal.

set the target attribute of your element to «_tab»

EDIT: It works, however W3Schools says there is no such target attribute: http://www.w3schools.com/tags/att_a_target.asp

EDIT2: From what I’ve figured out from the comments. setting target to _blank will take you to a new tab or window (depending on your browser settings). Typing anything except one of the ones below will create a new tab group (I’m not sure how these work):

_blank Opens the linked document in a new window or tab _self Opens the linked document in the same frame as it was clicked (this is default) _parent Opens the linked document in the parent frame _top Opens the linked document in the full body of the window framename Opens the linked document in a named frame 

Источник

Оцените статью