cross-browser resize browser window in JavaScript

前端 未结 3 408
忘掉有多难
忘掉有多难 2020-12-01 07:50

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 majo

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 08:35

    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).

    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.

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

    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);
    }
    

提交回复
热议问题