简体   繁体   English

JavaScript 在新窗口中打开,而不是选项卡

[英]JavaScript open in a new window, not tab

I have a select box that calls window.open(url) when an item is selected.我有一个选择框,它在选择项目时调用window.open(url) Firefox will open the page in a new tab by default.默认情况下,Firefox 将在新选项卡中打开该页面。 However, I would like the page to open in a new window, not a new tab.但是,我希望页面在新窗口中打开,而不是在新选项卡中打开。

How can I accomplish this?我怎样才能做到这一点?

Specify window "features" to the open call:open调用指定窗口“功能”:

window.open(url, windowName, "height=200,width=200");

When you specify a width/height, it will open it in a new window instead of a tab.当您指定宽度/高度时,它将在新窗口而不是选项卡中打开它。

See https://developer.mozilla.org/en-US/docs/Web/API/Window.open#Position_and_size_features for all the possible features.有关所有可能的功能,请参阅https://developer.mozilla.org/en-US/docs/Web/API/Window.open#Position_and_size_features

You don't need to use height, just make sure you use _blank , Without it, it opens in a new tab.您不需要使用高度,只需确保使用_blank ,没有它,它会在新选项卡中打开。

For a empty window:对于空窗口:

window.open('', '_blank', 'toolbar=0,location=0,menubar=0');

For a specific URL:对于特定的 URL:

window.open('http://www.google.com', '_blank', 'toolbar=0,location=0,menubar=0');

我可能是错的,但据我所知,这是由用户的浏览器首选项控制的,我不相信这可以被覆盖。

Try:尝试:

window.open("", [window name], "height=XXX,width=XXX,modal=yes,alwaysRaised=yes");

I have some code that does what your say, but there is a lot of parameters in it.我有一些代码可以做你所说的,但是里面有很多参数。 I think these are the bare minimum, let me know if it doesn't work, I'll post the rest.我认为这些是最低限度的,如果它不起作用,请告诉我,我会发布其余的。

OK, after making a lot of test, here my concluson:好的,经过大量测试后,我的结论是:

When you perform:当您执行:

     window.open('www.yourdomain.tld','_blank');
     window.open('www.yourdomain.tld','myWindow');

or whatever you put in the destination field, this will change nothing: the new page will be opened in a new tab (so depend on user preference)或者无论您在目标字段中放置什么,这都不会改变任何内容:新页面将在新选项卡中打开(因此取决于用户偏好)

If you want the page to be opened in a new "real" window, you must put extra parameter.如果要在新的“真实”窗口中打开页面,则必须添加额外的参数。 Like:喜欢:

window.open('www.yourdomain.tld', 'mywindow','location=1,status=1,scrollbars=1, resizable=1, directories=1, toolbar=1, titlebar=1');

After testing, it seems the extra parameter you use, dont' really matter: this is not the fact you put "this parameter" or "this other one" which create the new "real window" but the fact there is new parameter(s).经过测试,您使用的额外参数似乎并不重要:这不是您放置“这个参数”或“另一个”创建新“真实窗口”的事实,而是存在新参数的事实)。

But something is confused and may explain a lot of wrong answers:但有些东西令人困惑,可能会解释很多错误的答案:

This:这:

 win1 = window.open('myurl1', 'ID_WIN');
 win2 = window.open('myurl2', 'ID_WIN', 'location=1,status=1,scrollbars=1');

And this:和这个:

 win2 = window.open('myurl2', 'ID_WIN', 'location=1,status=1,scrollbars=1');
 win1 = window.open('myurl1', 'ID_WIN');

will NOT give the same result.不会给出相同的结果。

In the first case, as you first open a page without extra parameter, it will open in a new tab .在第一种情况下,当您第一次打开一个没有额外参数的页面时,它将在一个新选项卡中打开。 And in this case, the second call will be also opened in this tab because of the name you give.在这种情况下,由于您提供的名称,第二个调用也将在此选项卡中打开。

In second case, as your first call is made with extra parameter, the page will be opened in a new " real window ".在第二种情况下,由于您的第一次调用是使用额外参数进行的,因此页面将在新的“真实窗口”中打开 And in that case, even if the second call is made without the extra parameter, it will also be opened in this new " real window "... but same tab!在这种情况下,即使在没有额外参数的情况下进行第二次调用,它也会在这个新的“真实窗口”中打开……但是相同的选项卡!

This mean the first call is important as it decided where to put the page.这意味着第一次调用很重要,因为它决定了页面的放置位置。

You might try following function:您可以尝试以下功能:

<script type="text/javascript">
function open(url)
{
  var popup = window.open(url, "_blank", "width=200, height=200") ;
  popup.location = URL;
}
</script>

The HTML code for execution:执行的HTML代码:

<a href="#" onclick="open('http://www.google.com')">google search</a>

You shouldn't need to.你不应该需要。 Allow the user to have whatever preferences they want.允许用户拥有他们想要的任何偏好。

Firefox does that by default because opening a page in a new window is annoying and a page should never be allowed to do so if that is not what is desired by the user. Firefox 默认会这样做,因为在新窗口中打开页面很烦人,如果用户不希望这样,则绝不应允许页面这样做。 (Firefox does allow you to open tabs in a new window if you set it that way). (如果您这样设置,Firefox 确实允许您在新窗口中打开选项卡)。

The key is the parameters :关键是参数:

If you provide Parameters [ Height="" , Width="" ] , then it will open in new windows.如果您提供参数 [ Height="" , Width="" ] ,则它将在新窗口中打开。

If you DON'T provide Parameters , then it will open in new tab.如果您不提供Parameters ,则它将在新选项卡中打开。

Tested in Chrome and Firefox在 Chrome 和 Firefox 中测试

Interestingly, I found that if you pass in an empty string (as opposed to a null string, or a list of properties) for the third attribute of window.open, it would open in a new tab for Chrome, Firefox, and IE.有趣的是,我发现如果为 window.open 的第三个属性传入一个字符串(而不是空字符串或属性列表),它将在 Chrome、Firefox 和 IE 的新选项卡中打开。 If absent, the behavior was different.如果不存在,行为就会不同。

So, this is my new call:所以,这是我的新电话:

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

try that method.....试试那个方法.....

function popitup(url) {
       //alert(url);
       newwindow=window.open("http://www.zeeshanakhter.com","_blank","toolbar=yes,scrollbars=yes, resizable=yes, top=500, left=500, width=400, height=400");
       newwindow.moveTo(350,150);
   if (window.focus) 
          {
             newwindow.focus()
          }
   return false;
  }

For me the solution was to have对我来说,解决方案是有

"location=0"

in the 3rd parameter.在第三个参数中。 Tested on latest FF/Chrome and an old version of IE11在最新的 FF/Chrome 和旧版本的 IE11 上测试

Full method call I use is below (As I like to use a variable width):我使用的完整方法调用如下(因为我喜欢使用可变宽度):

window.open(url, "window" + id, 'toolbar=0,location=0,scrollbars=1,statusbar=1,menubar=0,resizable=1,width=' + width + ',height=800,left=100,top=50');

I just tried this with IE (11) and Chrome (54.0.2794.1 canary SyzyASan):我刚刚用 IE (11) 和 Chrome (54.0.2794.1 canary SyzyASan) 尝试了这个:

window.open(url, "_blank", "x=y")

... and it opened in a new window. ...它在一个新窗口中打开。

Which means that Clint pachl had it right when he said that providing any one parameter will cause the new window to open.这意味着Clint pachl说得对,他说提供任何一个参数都会导致新窗口打开。

-- and apparently it doesn't have to be a legitimate parameter! - 显然它不必是一个合法的参数!

(YMMV - as I said, I only tested it in two places...and the next upgrade might invalidate the results, any way) (YMMV - 正如我所说,我只在两个地方测试了它......并且下一次升级可能会使结果无效,无论如何)

ETA: I just noticed, though - in IE, the window has no decorations. ETA:我刚刚注意到 - 在 IE 中,窗口没有装饰。

Answered here .在这里回答。 But posting it again for reference.不过再贴一次供参考。

window.open() will not open in new tab if it is not happening on actual click event.如果实际点击事件中没有发生window.open()将不会在新选项卡中打开。 In the example given the url is being opened on actual click event.在给出的示例中,url 是在实际点击事件上打开的。 This will work provided user has appropriate settings in the browser.如果用户在浏览器中有适当的设置,这将起作用。

<a class="link">Link</a>
<script  type="text/javascript">
     $("a.link").on("click",function(){
         window.open('www.yourdomain.com','_blank');
     });
</script>

Similarly, if you are trying to do an ajax call within the click function and want to open a window on success, ensure you are doing the ajax call with async : false option set.同样,如果您尝试在 click 函数中进行 ajax 调用并希望在成功时打开一个窗口,请确保您正在使用async : false选项集进行 ajax 调用。

I think its not html target properties problem but you unchecked "open nw windows in a new tab instead" option in "tab" tab under firefox "options" menu.我认为这不是 html 目标属性问题,但您在 firefox“选项”菜单下的“选项卡”选项卡中取消选中“在新选项卡中打开 nw 窗口”选项。 check it and try again.检查它并重试。

在此处输入图片说明

I had this same question but found a relatively simple solution to it.我有同样的问题,但找到了一个相对简单的解决方案。

In JavaScript I was checking for window.opener !=null;在 JavaScript 中,我正在检查window.opener !=null; to determine if the window was a pop up.以确定窗口是否弹出。 If you're using some similar detection code to determine if the window you're site is being rendered in is a pop up you can easily "turn it off" when you want to open a "new" window using the new windows JavaScript.如果您使用一些类似的检测代码来确定正在呈现您站点的窗口是否是一个弹出窗口,当您想使用新的 windows JavaScript 打开“新”窗口时,您可以轻松地“将其关闭”

Just put this at the top of your page you want to always be a "new" window.只需将它放在页面顶部,您就希望它始终是一个“新”窗口。

<script type="text/javascript">
    window.opener=null;
</script>

I use this on the log in page of my site so users don't get pop up behavior if they use a pop up window to navigate to my site.我在我网站的登录页面上使用它,这样用户在使用弹出窗口导航到我的网站时不会出现弹出行为。

You could even create a simple redirect page that does this and then moves to the URL you gave it.您甚至可以创建一个简单的重定向页面来执行此操作,然后移动到您提供的 URL。 Something like,就像是,

JavaScript on parent page:父页面上的 JavaScript:

window.open("MyRedirect.html?URL="+URL, "_blank");

And then by using a little javascript from here you can get the URL and redirect to it.然后通过使用这里的一些 javascript,您可以获得 URL 并重定向到它。

JavaScript on Redirect Page:重定向页面上的 JavaScript:

 <script type="text/javascript">
        window.opener=null;

    function getSearchParameters() {
          var prmstr = window.location.search.substr(1);
          return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {};
    }

    function transformToAssocArray( prmstr ) {
        var params = {};
        var prmarr = prmstr.split("&");
        for ( var i = 0; i < prmarr.length; i++) {
            var tmparr = prmarr[i].split("=");
            params[tmparr[0]] = tmparr[1];
        }
        return params;
    }

    var params = getSearchParameters();
    window.location = params.URL;
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM