Nauman Bashir
Nauman Bashir

Reputation: 1752

how to force a link to be opened in a new tab using javascript

i want to make sure that in all tab enabled browsers, when a user clicks a link, it opens in a new tab. All i have got so far is the target keyword in the anchor, but is there some new html attribute that supports that function?

Upvotes: 8

Views: 3881

Answers (4)

Sarfraz
Sarfraz

Reputation: 382696

There is no guaranteed way for that because you can change window opening behavior and tabbing options from within browser options.

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

Upvotes: 6

T.J. Crowder
T.J. Crowder

Reputation: 1074276

No, there's no HTML attribute that tells tab-enabled browsers to do this if they can, you're stuck with target using either "_blank" for new window or a specific name if you want to reuse a window. It would sure be nice, but there's not even anything in the current HTML5 working draft, at least not under a or target (e.g., there's no "context name" for "new tab").

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

Upvotes: 1

Dr.Molle
Dr.Molle

Reputation: 117334

There is a CSS3-property target-new

Unfortunately it isn't supported yet by any browser(I don't know any). But maybe you could already implement it for future use.

Upvotes: 4

August Lilleaas
August Lilleaas

Reputation: 54593

Your best bet is <a href="..." target="_blank"></a>.

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

There is also no vendor specific way of doing this that I know of.

Upvotes: 1

Related Questions