简体   繁体   English

Javascript:保存和还原浏览器窗口大小

[英]Javascript: save and restore browser window sizes

In my vaadin application, user can save a user workspace with multiple browser popup windows and restore the workspace later. 在我的vaadin应用程序中,用户可以保存具有多个浏览器弹出窗口的用户工作区,并在以后还原该工作区。

I save the browser window size returned by following vaadin methods. 我保存通过以下vaadin方法返回的浏览器窗口大小。

Page.getBrowserWindowHeight();
Page.getBrowserWindowWidth();

Vaadin returns the content width and height of the browser in above methods, not including title bar and other controls in the top such as the address bar and toolbar. Vaadin通过以上方法返回浏览器的内容宽度和高度,但不包括标题栏和顶部的其他控件(如地址栏和工具栏)。

When restoring a workspace, I use resizeTo() javascript method to set the size of the browser windows. 恢复工作区时,我使用resizeTo() javascript方法来设置浏览器窗口的大小。

JavaScript.getCurrent().execute("resizeTo(" + windowWidth + "," + windowHeight + ")");

resizeTo() sets the total width and height of the browser window, not the content area. resizeTo()设置浏览器窗口而不是内容区域的总宽度和高度。 Due to this, the browser window becomes smaller than the intended size. 因此,浏览器窗口变得小于预期的大小。

Does anyone know of a better method to achieve this? 有谁知道实现此目标的更好方法?

The methods you are using 您正在使用的方法

Page.getBrowserWindowHeight();
Page.getBrowserWindowWidth();

use the following JavaScript to determine the 'UI' dimensions 使用以下JavaScript确定“ UI”尺寸

// Window height and width
var cw = 0;
var ch = 0;
if (typeof(window.innerWidth) == 'number') {
    // Modern browsers
    cw = window.innerWidth;
    ch = window.innerHeight;
} else {
    // IE 8
    cw = document.documentElement.clientWidth;
    ch = document.documentElement.clientHeight;
} 

To do this using resizeTo() (although I think you may run in to some browser compatibility issues ) you need to take into account a few other thing: 要使用resizeTo()进行此操作(尽管我认为您可能会遇到一些浏览器兼容性问题 ),但您需要考虑其他一些事项:

function doResize(width, height){
    // Window height and width
    var cw = 0;
    var ch = 0;
    if(typeof(window.innerWidth) == 'number') {
        // Modern browsers
        cw = width + (window.outerWidth - window.innerWidth);
        ch = height + (window.outerHeight - window.innerHeight);
    } else {
        // IE 8
        cw = width + (document.documentElement.offsetWidth-document.body.offsetWidth);
        ch = height + (document.documentElement.offsetHeight-document.body.offsetHeight); 
    }
    window.resizeTo(cw, ch);
}

Another option would be to create a hidden component with RPC to do your own JS browser size calculations. 另一种选择是使用RPC创建一个隐藏的组件来执行您自己的JS浏览器大小的计算。

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

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