2011-10-28 62 views

回答

36

Web浏览器会自动聚焦新选项卡上,但你可以叫焦点回到:

function openWindow(url) 
{ 
    window.open(url, '_blank'); 
    window.focus(); 
} 

<a href="http://www.example.com/" onclick="javascript:openWindow(this.href);return false;">Click Me</a> 
+1

重新聚焦:根据我的经验没有。无论如何(http://jsbin.com/aboluk/2)不是Chrome,但它们在尝试中没有多大的伤害。 –

+0

@ T.J.Crowder:这对我来说在Chrome 15中确实有效。 – josh3736

+3

@ josh3736:这很有趣。我仍然在14(在Linux上),但它没有。所以基本上:你不能依靠它。 :) –

10

不幸的是,你目前无法做到这一点 - 但你可以关闭。您可以打开一个新的窗口,如果您没有指定任何窗口尺寸或窗口功能,那么大多数现代浏览器都会打开一个新选项卡(取决于用户的偏好,但随后您希望执行用户喜欢的操作无论如何吧?)。所以只需要window.open(url)window.open(url, name),如果你打算使用某个名字的话。一定要直接回应用户发起的事件,否则浏览器的弹出式窗口拦截器可能会...阻止弹出窗口。 :-)

Live example

关于保持专注在你窗上...祝你好运。您可以拨window.focus()window.open(...),但根据我的经验,这通常不起作用。

把它扔在那里:如果您的用户与URL的真正联系相互作用的东西,用户可以决定是否要在新标签中打开一个新的窗口,无论是否给它焦点(如果它们足够复杂,可以知道Shift + Click和Ctrl + Shift + Click或右键单击菜单)。

+0

注意,在IE浏览器,这确实打开一个新的窗口,而不是一个新的选项卡。 – josh3736

+0

@ josh3736:这取决于你使用的是什么版本的IE,在新版本中,你的设置是什么。 –

+0

忘记我在IE9中更改了该设置。具体来说,'window.open()'会在IE中产生一个新标签,除非选项>标签设置>“让IE决定弹出窗口应该如何打开”改变了。 – josh3736

2

这是用户特定的设置,你不能从JS改变这种行为。

3

不幸的是,你不能在所有的浏览器做到这一点,但如果你能做到这一点在Chrome实现浏览器的扩展。 如何通过JavaScript和标签操作在Chrome:

http://code.google.com/chrome/extensions/tabs.html

chrome.tabs.create(object createProperties, function callback) 
    Creates a new tab. Note: This function can be used without requesting the 'tabs' permission in the manifest. 
Parameters 
    **createProperties** (object) 
    **windowId** (optional integer) 
     The window to create the new tab in. Defaults to the current window. 
    **index** (optional integer) 
     The position the tab should take in the window. The provided value will be clamped to between zero and the number of tabs in the window. 
    **url** (optional string) 
     The URL to navigate the tab to initially. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page. 
    **selected** (optional boolean) 
     Whether the tab should become the selected tab in the window. Defaults to true 
    pinned (optional boolean) 
     Whether the tab should be pinned. Defaults to false 
    **callback** (optional function) 
1
(function(a){ 
document.body.appendChild(a); 
a.setAttribute('href', location.href); 
a.dispatchEvent((function(e){ 
    e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null); 
    return e 
}(document.createEvent('MouseEvents'))))}(document.createElement('a'))) 
1

邮政是旧的,但没有任何100%正确的答案。

解所有的浏览器:

你可以打开新标签页中同一域中通过网址:

window.open("newurl.php", "_blank"); 

和跨域新的标签,你必须创建一个简单的本地脚本,如这样的:

在新文件

“redirect.php”你传递一个参数(目标新标签的URL)这样

<?php 
    $newURL=$_GET['u']; 
    header('Location: '.$newURL); 
؟> 

,你可以与任何目标URL创建一个新的标签页中:

window.open("redirect.php?u=http://www.google.com", "_blank"); 

:)

的我在我的网站的解决方案。与任何

1

工作对我来说,使用

window.open("https://stackoverflow.com"); 

完美。这取决于您使用的浏览器,操作系统和个人偏好。

Chrome,Internet Explorer和Firefox都打开了一个新选项卡。对不起,如果这不适合你。

(就意识到,这是一个古老的线程)

相关问题