How to make a link open in a new tab using javascript

I want to make sure that in all browsers with the tab turned on, when the user clicks on the link, it opens in a new tab. All I got so far is the target keyword in the anchor, but is there any new html attribute that supports this function?

+6
javascript html
source share
4 answers

There is no guaranteed way to do this, because you can change the window opening behavior and tab settings from the browser settings.

The best you can do is write code using target="_blank" or window.open() .

+5
source share

There is a CSS3 property target-new

Unfortunately, it is not supported by any browser (I do not know). But perhaps you can already implement it for future use.

+3
source share

Best of all is <a href="..." target="_blank"></a> .

There is no standardized semantic way to tell the browser to open a new tab. This is because not all browsers have tabs. Take most mobile browsers, for example, they have no tabs.

There is also no specific way to create this that I know of.

+1
source share

No, there is no HTML attribute that tells tab-enabled browsers if they can, you are stuck with target , using either "_blank" for the new window, or a specific name if you want to reuse the window. This would be nice, but there was nothing in the current draft HTML5 working document, at least not under a or target (for example, there is a โ€œcontext nameโ€ for a โ€œnew tabโ€).

Edit : But see (and vote) Dr.Molle's answer . CSS to the rescue (someday)!

+1
source share

All Articles