Chrome、Javascript、window.open 在新标签页中打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15818892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome, Javascript, window.open in new tab
提问by Andrew
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?
这可行吗?
回答by Prakash Chennupati
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.您可以通过在第三个参数中指定选项来强制浏览器使用新窗口('popup')
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.如果用户发起的事件委托或推迟了函数调用(在未绑定到点击事件的事件侦听器或委托中,或例如使用 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);
回答by Frank A Tinker
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.
如果用户喜欢,强制使用选项卡有时很有用。正如上面的 Prakash 所说,这有时是由使用非用户启动的事件决定的,但有一些方法可以解决这个问题。
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”,因为“始终”功能不被视为用户启动的。但是,如果我们这样做:
$("#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。瞧,如果用户喜欢,我们会强制使用选项卡。
回答by Mohammed Safeer
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/
为 ajax 窗口打开工作 js 小提琴http://jsfiddle.net/safeeronline/70kdacL4/1/
回答by Nico Wiedemann
At the moment (Chrome 39) I use this code to open a new tab:
目前(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.
当然,这可能会在未来版本的 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.
如果您无法控制用户使用的浏览器,那么使用它是一个坏主意。它可能不适用于未来版本或不同的设置。
回答by Magne
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 请求之后),但是浏览器不会将其识别为用户启动的点击事件,并且可能会阻止弹出窗口。
回答by Serhii Matrunchyk
Clear mini-solution $('<form action="http://samedomainurl.com/" target="_blank"></form>').submit()
清晰的迷你解决方案 $('<form action="http://samedomainurl.com/" target="_blank"></form>').submit()
回答by Dilip Rajkumar
You can use this code to open in new tab..
您可以使用此代码在新选项卡中打开..
function openWindow( url )
{
window.open(url, '_blank');
window.focus();
}
I got it from stackoverflow..
我从stackoverflow得到它..
回答by Mohammad Shraim
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 函数