在 JavaScript 中跨浏览器调整浏览器窗口大小

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

cross-browser resize browser window in JavaScript

javascriptbrowsercross-browser

提问by Web_Designer

I would like to be able to resize the browser window with JavaScript. I don't want to use jQuery, and the smaller the code the better, but it has to work in all of the major browsers including Chrome.

我希望能够使用 JavaScript 调整浏览器窗口的大小。我不想使用 jQuery,代码越小越好,但它必须适用于包括 Chrome 在内的所有主要浏览器。

Any solutions?

任何解决方案?

Thanks for the help!

谢谢您的帮助!

P.S. The tags that this question was tagged with should be combined but I don't know how.

PS 这个问题被标记的标签应该组合,但我不知道如何。

browser -----------same as--> webbrowser

浏览器-----------同--> webbrowser

cross-browser----same as--> browser compatibility

跨浏览器----同-->浏览器兼容性

采纳答案by epascarello

window.resizeTo( width, height );

window.resizeTo( 宽度, 高度 );

The problem you may face is modern day browsers can prevent you in the settings to not be able to resize the window. There is no way around that.

您可能面临的问题是现代浏览器可以阻止您在设置中无法调整窗口大小。没有办法解决这个问题。

Chrome will not allow it. Won't Fix Bug

Chrome 不会允许。不会修复错误

IE is based on security zones

IE 基于安全区域

回答by tivolimeister

The fact is that Chrome, Firefox and newer IEs doesn't allow resize of tabbed windows and windows not opened by window.open()(Chrome at least).

事实是,Chrome、Firefox 和更新的 IE 不允许调整选项卡式窗口和未由window.open()(至少是Chrome)打开的窗口的大小。

But for popups it's doable for the most part, unless the security setting in the browser blocks that functionality. However using window.resizeTo()is complicated. Use window.resizeBy()instead.

但是对于弹出窗口,它在大多数情况下是可行的,除非浏览器中的安全设置阻止了该功能。但是使用起来window.resizeTo()很复杂。使用window.resizeBy()来代替。

Chrome has a buggetting the size of a popup window to soon so you have to work around that.

Chrome 有一个错误,很快就会使弹出窗口的大小变大,因此您必须解决这个问题。

if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
    var t = setTimeout("resize()", 200);
else
    resize();

function resize() {
    var innerWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var innerHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    var targetWidth = 800;
    var targetHeight = 600;
    window.resizeBy(targetWidth-innerWidth, targetHeight-innerHeight);
}

回答by duskwuff -inactive-

There is no way for a web page to resize the main browser window in Chrome. JavaScript code is only permitted to resize popup windows.

网页无法在 Chrome 中调整主浏览器窗口的大小。JavaScript 代码只允许调整弹出窗口的大小。