Javascript 我可以调整浏览器窗口的大小吗?

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

Can I resize the browser window?

javascriptjquerybrowser

提问by Sanghyun Lee

I want to resize a browser popup window's width and height. I can set the popup window size but I want to convert the window size fit to contents size when a page is redirected to another page.

我想调整浏览器弹出窗口的宽度和高度。我可以设置弹出窗口大小,但是当页面重定向到另一个页面时,我想将适合的窗口大小转换为内容大小。

I tried this:

我试过这个:

$(window).width(1000);  // Not working.

How can I do this?

我怎样才能做到这一点?

Update

更新

Everybody told me not to do it. To find out the exact reason why this is bad practice, I asked on ux.stackexchange.com.

每个人都告诉我不要这样做。为了找出这是不好做法的确切原因,我在ux.stackexchange.com询问

回答by Sanghyun Lee

I found some answers to this question. This question is duplicate. But I'll answer again because I'll integrate them and add some more information.

我找到了这个问题的一些答案。这个问题是重复的。但我会再次回答,因为我会整合它们并添加更多信息。

To resize the window, you can simply do this:

要调整窗口大小,您只需执行以下操作:

window.resizeTo(width, height);

But this method is not working in Chrome and Opera. How do you resize an IE browser window to 1024 x 768

但是这种方法在 Chrome 和 Opera 中不起作用。如何将 IE 浏览器窗口的大小调整为 1024 x 768

The reason, from The javascript "resizeTo" function not working in Chrome and Opera, is:

来自javascript "resizeTo" 函数在 Chrome 和 Opera 中不起作用的原因是:

The resizeTo method is disabled by default in several browsers, and I know that it can also be manually disabled in Firefox.

It has been widely misused, so most browser vendors feel that it should be disabled, or at least a user controllable option.

resizeTo 方法在几个浏览器中默认是禁用的,我知道它也可以在 Firefox 中手动禁用。

它已被广泛滥用,因此大多数浏览器供应商认为应该禁用它,或者至少是用户可控的选项。

But the method is working on popups even in Chrome or Opera. I guess the reason is that the browser vendors thought the resizeTomethod could be needed in popups, and I think so.

但即使在 Chrome 或 Opera 中,该方法也适用于弹出窗口。我想原因是浏览器供应商认为resizeTo在弹出窗口中可能需要该方法,我认为是这样。

To discuss this I made a thread about this issue on ux.stackexchange.com. https://ux.stackexchange.com/questions/9903/why-is-resizing-the-browser-window-bad-practice/9962#9962

为了讨论这个问题,我在 ux.stackexchange.com 上写了一个关于这个问题的帖子。 https://ux.stackexchange.com/questions/9903/why-is-resizing-the-browser-window-bad-practice/9962#9962

I'm not sure if the resizeTomethod is evil in every case but I'm sure that one should be careful when using this method.

我不确定这种resizeTo方法是否在每种情况下都是邪恶的,但我确信在使用这种方法时应该小心。

回答by Chhaya

<script type="text/javascript">
    function changeScreenSize() {        
        window.resizeTo(screen.width-300,screen.height-500)   
    }
</script>

<body onload="changeScreenSize()">

回答by sina72

use

window.resizeBy(relativeW, relativeH);

回答by Ira Rainey

Whilst you might technically be able to do this, you really shouldn't. forcing the size of the browser window fundamentally changes the browsing experience for a user which isn't your place to do. Very bad practise and poor user experience.

虽然您在技术上可能能够做到这一点,但您确实不应该这样做。强制浏览器窗口的大小从根本上改变了用户的浏览体验,而这不是您的职责所在。非常糟糕的做法和糟糕的用户体验。

Edit:Interesting to still get comments on this, but to be clear this answer is 6 years old, which is an age in web development time. Rather than edit it in context or delete it, I would say that it is wise to investigate currentweb development UX practises.

编辑:仍然对此发表评论很有趣,但要清楚这个答案是 6 岁,这是 Web 开发时间的一个年龄。与其在上下文中编辑或删除它,不如说调查当前的Web 开发用户体验实践是明智的。