23

If you are just here to tell me on why resizing a window is a bad idea, obnoxious, and the web-equivalent of nails on a chalk board, then please go away. I am looking for a solution not a lecture. This is to be used in a intranet setting only. Specifically, to debug and then demo responsively designed web sites (sites that use media queries to scale) to clients for review via a web meeting. I want to demo specific break points. This is not to be unleashed on the general public.

Things I already know:

  1. You can only resize and position windows opened via javascript, not by the user. (unless they adjust their security settings, which is not an option)
  2. When you use window's resizeTo() method, the browser is resized to the specified dimensions. The viewport is often significantly smaller (like 30 to 100 pixels smaller on average) However, I want to resize the viewport to a specific size.
  3. The browser, browser version, platform, some plugins, and various menu's and tool bars will alter the viewport's dimensions. More menus and tool bars means the viewport width is smaller.

Possible Solution:

  1. Find the browser's width and height
  2. Find the viewport's width and height
  3. Determine the difference between the two.
  4. Adjust the values in my call to resizeTo() accordingly

I need help with step 1. Everything else I can figure out. I'm also using jQuery and modernizer if that helps.

Thanks in advance for your help!

3
  • 1
    quirksmode.org/dom/w3c_cssom.html
    – SeanCannon
    Commented Apr 30, 2012 at 14:59
  • Only issue you will have with step 4 is making sure browsers have the setting enabled to open the pop up into a new window and that you are able to resize it. Commented Apr 30, 2012 at 15:15
  • I love the 1st 2 sentences of this!
    – dewd
    Commented Jul 13, 2023 at 14:13

1 Answer 1

29

The post is quite old, but here is a concrete implementation for future readers :

var resizeViewPort = function(width, height) {
    if (window.outerWidth) {
        window.resizeTo(
            width + (window.outerWidth - window.innerWidth),
            height + (window.outerHeight - window.innerHeight)
        );
    } else {
        window.resizeTo(500, 500);
        window.resizeTo(
            width + (500 - document.body.offsetWidth),
            height + (500 - document.body.offsetHeight)
        );
    }
};

And if you want to take the scrollbars into account :

var resizeViewPort = function(width, height) {
    var tmp = document.documentElement.style.overflow;
    document.documentElement.style.overflow = "scroll";

    if (window.outerWidth) {
        window.resizeTo(
            width + (window.outerWidth - document.documentElement.clientWidth),
            height + (window.outerHeight - document.documentElement.clientHeight)
        );
    } else {
        window.resizeTo(500, 500);
        window.resizeTo(
            width + (500 - document.documentElement.clientWidth),
            height + (500 - document.documentElement.clientHeight)
        );
    }

    document.documentElement.style.overflow = tmp;
};

Have fun...

2
  • 2
    Confirmed this works with IE 11, FF, and Chrome (all on Windows)
    – mlhDev
    Commented Mar 6, 2014 at 2:37
  • 3
    Excellent solution, however I used it in the window.resize event. The first one doesn't work well in that case in at least two desktop versions of Safari: the window keeps resizing and resizing, several times, until it either hits the right size, or it gets stuck in a resizing loop.What solved that for me, is computing window.outerWidth - window.innerWidth once, when the DOM was loaded, and using those values each time, instead of computing them on each resize. I did the same thing for the height.
    – toon81
    Commented Jun 27, 2014 at 12:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.