Chrome,Javascript,window.open in new tab

在 chrome 中,这会在一个新标签页中打开:

<button onclick="window.open('newpage.html', '_blank')" />

这将在一个新窗口中打开(但我希望这也在一个新标签页中打开:

<script language="javascript">
window.open('newpage.html', '_blank');
</script>

这可行吗?

308098 次浏览

你不能直接控制它,因为它是一个由 Internet Explorer 用户控制的选项。

使用不同窗口名称的 Window.open 打开页面将在新的浏览器窗口中打开,如果用户配置浏览器这样做的话,就像弹出窗口一样,或者在新的选项卡中打开。

编辑:

A more detailed explanation:

在现代浏览器中,window.open 会在一个新的标签页中打开,而不是弹出窗口。

2. You can force a browser to use a new window (‘popup’) by specifying options in the 3rd parameter

3. 如果 window.open 调用不是用户发起的事件的一部分,它将在新窗口中打开。

4.“用户发起的事件”不必调用相同的函数-但它必须来自用户单击调用的函数

5.如果用户发起的事件委托或推迟函数调用(在事件侦听器中,或者没有绑定到 click 事件的委托,或者使用 setTimeout) ,它将失去“用户发起”的状态

有些弹出窗口阻止程序允许从用户发起的事件中打开窗口,但是不允许以其他方式打开窗口。

7.如果任何弹出窗口被阻止,那些通常被阻止者允许的(通过用户发起的事件)有时也会被阻止。 一些例子..。

强制在新的浏览器实例中打开窗口,而不是新的选项卡:

window.open('page.php', '', 'width=1000');

以下内容符合用户发起的事件的条件,即使它调用了另一个函数:

function o(){
window.open('page.php');
}
$('button').addEvent('click', o);

由于 setTimeout 将其推迟,以下内容不符合用户发起的事件的条件:

function g(){
setTimeout(o, 1);
}
function o(){
window.open('page.php');
}
$('button').addEvent('click', g);

您可以使用此代码在新选项卡中打开. 。

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

我从 堆栈溢出买的。

It is sometimes useful to force the use of a tab, if the user likes that. As Prakash stated above, this is sometimes dictated by the use of a non-user-initiated event, but there are ways around that.

例如:

$("#theButton").button().click( function(event) {
$.post( url, data )
.always( function( response ) {
window.open( newurl + response, '_blank' );
} );
} );

将总是在新的浏览器窗口中打开“ newurl”,因为“ always”函数不被认为是由用户发起的。然而,如果我们这样做:

$("#theButton").button().click( function(event) {
var newtab = window.open( '', '_blank' );
$.post( url, data )
.always( function( response ) {
newtab.location = newurl + response;
} );
} );

我们打开新的浏览器窗口或创建新的选项卡,这由用户在按钮中的首选项决定,按钮是用户发起的。然后,在从 AJAX 帖子返回之后,我们将位置设置为所需的 URL。瞧,如果用户喜欢,我们就强制使用标签。

清除迷你解决方案 $('<form action="http://samedomainurl.com/" target="_blank"></form>').submit()

我使用的最佳方法:

1-add link to your html:

<a id="linkDynamic" target="_blank" href="#"></a>

2- add JS function:

function OpenNewTab(href)
{
document.getElementById('linkDynamic').href = href;
document.getElementById('linkDynamic').click();
}

3-只要调用 OpenNewTab 函数的链接,你想要的

这将在 Chrome 和 Firefox 的新标签页中打开链接,可能还有更多我没有测试过的浏览器:

var popup  = $window.open("about:blank", "_blank"); // the about:blank is to please Chrome, and _blank to please Firefox
popup.location = 'newpage.html';

它基本上打开一个新的空选项卡,然后设置该空选项卡的位置。请注意,这是一种黑客行为,因为浏览器标签/窗口行为实际上是浏览器和用户的领域、责任和选择。

第二行可以在回调中调用(例如,在您执行了一些 AJAX 请求之后) ,但是浏览器不会将其识别为用户发起的单击事件,并且可能会阻止弹出窗口。

现在(Chrome 39)我用这段代码打开一个新标签:

window.open('http://www.stackoverflow.com', '_blank', 'toolbar=yes, location=yes, status=yes, menubar=yes, scrollbars=yes');

Of course this may change in future versions of Chrome.

It is a bad idea to use this if you can't control the browser your users are using. It may not work in future versions or with different settings.

if you use window.open(url, '_blank') , it will be blocked(popup blocker) on Chrome,Firefox etc

试试这个,

$('#myButton').click(function () {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
});

工作 js 小提琴为这个 http://jsfiddle.net/safeeronline/70kdacL4/2/

工作 js 小提琴的 Ajax 窗口打开 http://jsfiddle.net/safeeronline/70kdacL4/1/