Javascript 如何用Javascript打开最大化的窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10297168/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:43:54  来源:igfitidea点击:

How to open maximized window with Javascript?

javascriptinternet-explorer

提问by M Sach

I'm using Javascript's self.open()to open a link in a new window and i'd like that window to be maximized. I tried the fullscreen=yesoption, which really doesn't do what I want. I am using below code:

我正在使用 Javascriptself.open()在新窗口中打开一个链接,我希望该窗口最大化。我尝试了该fullscreen=yes选项,但它确实不能满足我的要求。我正在使用以下代码:

self.open(pageLoc,popUpName,'height=1600,width=1800,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes'); 

If i also mention fullscreen=yes, then window opens up like you press F11. But i don't want it that. What i want is when i double click on IEand click maximize icon on top right corner.

如果我也提到fullscreen=yes,那么窗口会像按 F11 一样打开。但我不想那样。我想要的是当我双击IE并单击右上角的最大化图标时。

As i have given the heightand widthvalue so large, it is close to maximized window but not actual maximized window. (the reason i am saying this because even now if i click maximize button, it further expans little bit)

由于我已经给出了如此大的heightwidth值,它接近最大化窗口但不是实际最大化窗口。(我之所以这么说是因为即使现在我点击最大化按钮,它也会进一步扩展一点)

回答by ninjagecko

var params = [
    'height='+screen.height,
    'width='+screen.width,
    'fullscreen=yes' // only works in IE, but here for completeness
].join(',');
     // and any other options from
     // https://developer.mozilla.org/en/DOM/window.open

var popup = window.open('http://www.google.com', 'popup_window', params); 
popup.moveTo(0,0);

Please refrain from opening the popup unless the user really wants it, otherwise they will curse you and blacklist your site. ;-)

除非用户真的想要它,否则请不要打开弹出窗口,否则他们会诅咒您并将您的网站列入黑名单。;-)

edit: Oops, as Joren Van Severen points out in a comment, this may not take into account taskbars and window decorations (in a possibly browser-dependent way). Be aware. It seems that ignoring height and width (only param is fullscreen=yes) seems to work on Chrome and perhaps Firefox too; the original 'fullscreen' functionality has been disabled in Firefox for being obnoxious, but has been replaced with maximization. This directly contradicts information on the same page of https://developer.mozilla.org/en/DOM/window.openwhich says that window-maximizing is impossible. This 'feature' may or may not be supported depending on the browser.

编辑:糟糕,正如 Joren Van Severen 在评论中指出的那样,这可能没有考虑到任务栏和窗口装饰(以可能依赖于浏览器的方式)。意识到。似乎忽略高度和宽度(只有 param is fullscreen=yes)似乎适用于 Chrome,也许也适用于 Firefox;最初的“全屏”功能在 Firefox 中因令人讨厌而被禁用,但已被最大化取代。这直接与https://developer.mozilla.org/en/DOM/window.open同一页面上的信息相矛盾,该信息表示窗口最大化是不可能的。根据浏览器的不同,可能支持也可能不支持此“功能”。

回答by silly

 window.open('your_url', 'popup_name','height=' + screen.height + ',width=' + screen.width + ',resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes')

回答by Gyum Fox

The best solution I could find at present timeto open a window maximized is (Internet Explorer 11, Chrome 49, Firefox 45):

我能找到的最好的解决方案,目前的时间打开窗口最大化为器(Internet Explorer 11,铬49,火狐45):

  var popup = window.open("your_url", "popup", "fullscreen");
  if (popup.outerWidth < screen.availWidth || popup.outerHeight < screen.availHeight)
  {
    popup.moveTo(0,0);
    popup.resizeTo(screen.availWidth, screen.availHeight);
  }

see https://jsfiddle.net/8xwocrp6/7/

https://jsfiddle.net/8xwocrp6/7/

Note 1: It does not work on Edge (13.1058686). Not sure whether it's a bug or if it's as designed (I've filled a bug report, we'll see what they have to say about it). Here is a workaround:

注 1:它不适用于 Edge (13.1058686)。不确定这是一个错误还是设计的那样(我已经填写了一个错误报告,我们会看看他们对此有什么看法)。这是一个解决方法:

if (navigator.userAgent.match(/Edge\/\d+/g))
{
    return window.open("your_url", "popup", "width=" + screen.width + ",height=" + screen.height);
}

Note 2: moveToor resizeTowill not work (Access denied) if the window you are opening is on another domain.

注2moveToresizeTo将无法正常工作(拒绝访问)如果你是开放的窗口是在另一个领域。

回答by Kaushal PAtel

If I use Firefox then screen.widthand screen.heightworks fine but in IE and Chrome they don't work properly instead it opens with the minimum size.

如果我使用 Firefoxscreen.width并且screen.height工作正常,但在 IE 和 Chrome 中它们无法正常工作,而是以最小尺寸打开。

And yes I tried giving too large numbers too like 10000for both heightand widthbut not exactly the maximized effect.

是的,我尝试10000为两者提供太大的数字heightwidth但并不完全是最大化的效果。

回答by ThdK

Checkout this jquery window plugin: http://fstoke.me/jquery/window/

查看这个 jquery 窗口插件:http: //fstoke.me/jquery/window/

// create a window
sampleWnd = $.window({
   .....
});

// resize the window by passed w,h parameter
sampleWnd.resize(screen.width, screen.height);