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 i开发者_StackOverflown 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?
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()
.
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.
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.
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)!
精彩评论