Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

javascript - 在新标签页(而不是新窗口)中打开URL(Open a URL in a new tab (and not a new window))

I'm trying to open a URL in a new tab, as opposed to a popup window.

(我正在尝试在新选项卡(而不是弹出窗口)中打开URL 。)

I've seen related questions where the responses would look something like:

(我看过相关的问题,答案看起来像这样:)

window.open(url,'_blank');
window.open(url);

But none of them worked for me, the browser still tried to open a popup window.

(但是它们都不适合我,浏览器仍然尝试打开一个弹出窗口。)

  ask by Mark F translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a trick,

(这是一个把戏)

function openInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();
}

In most cases, this should happen directly in the onclick handler for the link to prevent pop-up blockers, and the default "new window" behavior.

(在大多数情况下,此操作应直接发生在链接的onclick处理程序中,以防止弹出窗口阻止程序和默认的“新窗口”行为。)

You could do it this way, or by adding an event listener to your DOM object.

(您可以通过这种方式来实现,也可以通过将事件侦听器添加到DOM对象中来实现。)

<div onclick="openInNewTab('www.test.com');">Something To Click On</div>

http://www.tutsplanet.com/open-url-new-tab-using-javascript/

(http://www.tutsplanet.com/open-url-new-tab-using-javascript/)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...