新标签中的Chrome,Javascript,window.open

时间:2022-10-22 15:16:57

In chrome this opens in a new tab:

在chrome中,这将在新选项卡中打开:

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

this opens in a new window (but I'd like this to open in a new tab as well:

这将在一个新窗口中打开(但我想在新标签页中打开它:

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

Is this feasible?

这可行吗?

8 个解决方案

#1


139  

You can't directly control this, because it's an option controlled by Internet Explorer users.

您无法直接控制它,因为它是由Internet Explorer用户控制的选项。

Opening pages using Window.open with a different window name will open in a new browser window like a popup, OR open in a new tab, if the user configured the browser to do so.

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

EDIT:

编辑:

A more detailed explanation:

更详细的解释:

1. In modern browsers, window.open will open in a new tab rather than a popup.

1.在现代浏览器中,window.open将在新选项卡而不是弹出窗口中打开。

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

2.您可以通过在第3个参数中指定选项来强制浏览器使用新窗口(“弹出窗口”)

3. If the window.open call was not part of a user-initiated event, it’ll open in a new window.

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

4. A “user initiated event” does not have to the same function call – but it must originate in the function invoked by a user click

4.“用户启动的事件”不必具有相同的函数调用 - 但它必须源自用户单击调用的函数

5. If a user initiated event delegates or defers a function call (in an event listener or delegate not bound to the click event, or by using setTimeout for example), it loses it’s status as “user initiated”

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

6. Some popup blockers will allow windows opened from user initiated events, but not those opened otherwise.

6.某些弹出窗口阻止程序将允许从用户启动的事件打开窗口,但不允许打开其他窗口。

7. If any popup is blocked, those normally allowed by a blocker (via user initiated events) will sometimes also be blocked. Some examples…

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

Forcing a window to open in a new browser instance, instead of a new tab:

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

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

The following would qualify as a user-initiated event, even though it calls another function:

以下将有资格作为用户启动的事件,即使它调用另一个函数:

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

The following would not qualify as a user-initiated event, since the setTimeout defers it:

以下不符合用户启动的事件,因为setTimeout推迟了它:

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

#2


31  

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.

如果用户喜欢,强制使用选项卡有时很有用。正如普拉卡什所述,这有时是由非用户发起的事件所决定的,但有很多方法可以解决这个问题。

For example:

例如:

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

will always open "newurl" in a new browser window since the "always" function is not considered user-initiated. However, if we do this:

将始终在新的浏览器窗口中打开“newurl”,因为“always”功能不被视为用户启动。但是,如果我们这样做:

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

we open the new browser window or create the new tab, as determined by the user preference in the button click which IS user-initiated. Then we just set the location to the desired URL after returning from the AJAX post. Voila, we force the use of a tab if the user likes that.

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

#3


10  

At the moment (Chrome 39) I use this code to open a new tab:

目前(Chrome 39)我使用此代码打开一个新标签:

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

Of course this may change in future versions of Chrome.

当然,这可能会在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.

如果您无法控制用户正在使用的浏览器,则使用此功能是一个坏主意。它可能无法在将来的版本或不同的设置中使用。

#4


8  

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

如果您使用window.open(url,'_ blank'),它将在Chrome,Firefox等上被阻止(弹出窗口拦截器)

try this,

尝试这个,

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

working js fiddle for this http://jsfiddle.net/safeeronline/70kdacL4/2/

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

working js fiddle for ajax window open http://jsfiddle.net/safeeronline/70kdacL4/1/

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

#5


6  

This will open the link in a new tab in Chrome and Firefox, and possibly more browsers I haven't tested:

这将在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';

It basically opens a new empty tab, and then sets the location of that empty tab. Beware that it is a sort of a hack, since browser tab/window behavior is really the domain, responsibility and choice of the Browser and the User.

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

The second line can be called in a callback (after you've done some AJAX request for example), but then the browser would not recognize it as a user-initiated click-event, and may block the popup.

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

#6


3  

Clear mini-solution $('<form action="http://samedomainurl.com/" target="_blank"></form>').submit()

清除迷你解决方案$('

')。submit()

#7


0  

You can use this code to open in new tab..

您可以使用此代码在新标签中打开..

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

I got it from *..

我从*得到它..

#8


-2  

Best way i use:

我使用的最佳方式:

1- add link to your html:

1-添加你的HTML链接:

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

2- add JS function:

2-添加JS功能:

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

3- just call OpenNewTab function with the link you want

3-只需使用您想要的链接调用OpenNewTab函数

#1


139  

You can't directly control this, because it's an option controlled by Internet Explorer users.

您无法直接控制它,因为它是由Internet Explorer用户控制的选项。

Opening pages using Window.open with a different window name will open in a new browser window like a popup, OR open in a new tab, if the user configured the browser to do so.

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

EDIT:

编辑:

A more detailed explanation:

更详细的解释:

1. In modern browsers, window.open will open in a new tab rather than a popup.

1.在现代浏览器中,window.open将在新选项卡而不是弹出窗口中打开。

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

2.您可以通过在第3个参数中指定选项来强制浏览器使用新窗口(“弹出窗口”)

3. If the window.open call was not part of a user-initiated event, it’ll open in a new window.

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

4. A “user initiated event” does not have to the same function call – but it must originate in the function invoked by a user click

4.“用户启动的事件”不必具有相同的函数调用 - 但它必须源自用户单击调用的函数

5. If a user initiated event delegates or defers a function call (in an event listener or delegate not bound to the click event, or by using setTimeout for example), it loses it’s status as “user initiated”

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

6. Some popup blockers will allow windows opened from user initiated events, but not those opened otherwise.

6.某些弹出窗口阻止程序将允许从用户启动的事件打开窗口,但不允许打开其他窗口。

7. If any popup is blocked, those normally allowed by a blocker (via user initiated events) will sometimes also be blocked. Some examples…

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

Forcing a window to open in a new browser instance, instead of a new tab:

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

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

The following would qualify as a user-initiated event, even though it calls another function:

以下将有资格作为用户启动的事件,即使它调用另一个函数:

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

The following would not qualify as a user-initiated event, since the setTimeout defers it:

以下不符合用户启动的事件,因为setTimeout推迟了它:

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

#2


31  

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.

如果用户喜欢,强制使用选项卡有时很有用。正如普拉卡什所述,这有时是由非用户发起的事件所决定的,但有很多方法可以解决这个问题。

For example:

例如:

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

will always open "newurl" in a new browser window since the "always" function is not considered user-initiated. However, if we do this:

将始终在新的浏览器窗口中打开“newurl”,因为“always”功能不被视为用户启动。但是,如果我们这样做:

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

we open the new browser window or create the new tab, as determined by the user preference in the button click which IS user-initiated. Then we just set the location to the desired URL after returning from the AJAX post. Voila, we force the use of a tab if the user likes that.

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

#3


10  

At the moment (Chrome 39) I use this code to open a new tab:

目前(Chrome 39)我使用此代码打开一个新标签:

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

Of course this may change in future versions of Chrome.

当然,这可能会在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.

如果您无法控制用户正在使用的浏览器,则使用此功能是一个坏主意。它可能无法在将来的版本或不同的设置中使用。

#4


8  

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

如果您使用window.open(url,'_ blank'),它将在Chrome,Firefox等上被阻止(弹出窗口拦截器)

try this,

尝试这个,

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

working js fiddle for this http://jsfiddle.net/safeeronline/70kdacL4/2/

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

working js fiddle for ajax window open http://jsfiddle.net/safeeronline/70kdacL4/1/

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

#5


6  

This will open the link in a new tab in Chrome and Firefox, and possibly more browsers I haven't tested:

这将在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';

It basically opens a new empty tab, and then sets the location of that empty tab. Beware that it is a sort of a hack, since browser tab/window behavior is really the domain, responsibility and choice of the Browser and the User.

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

The second line can be called in a callback (after you've done some AJAX request for example), but then the browser would not recognize it as a user-initiated click-event, and may block the popup.

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

#6


3  

Clear mini-solution $('<form action="http://samedomainurl.com/" target="_blank"></form>').submit()

清除迷你解决方案$('

')。submit()

#7


0  

You can use this code to open in new tab..

您可以使用此代码在新标签中打开..

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

I got it from *..

我从*得到它..

#8


-2  

Best way i use:

我使用的最佳方式:

1- add link to your html:

1-添加你的HTML链接:

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

2- add JS function:

2-添加JS功能:

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

3- just call OpenNewTab function with the link you want

3-只需使用您想要的链接调用OpenNewTab函数