Disable Browser Window Resize

我的未来我决定 提交于 2019-11-27 01:11:16

You can first determine a definite size.

var size = [window.width,window.height];  //public variable

Then do this:

$(window).resize(function(){
    window.resizeTo(size[0],size[1]);
});

Demo: http://jsfiddle.net/DerekL/xeway917/


Q: Won't this cause an infinite loop of resizing? - user1147171

Nice question. This will not cause an infinite loop of resizing. The W3C specification states that resize event must be dispatched only when a document view has been resized. When the resizeTo function try to execute the second time, the window will have the exact same dimension as it just set, and thus the browser will not fire the resize event because the dimensions have not been changed.

I needed to do this today (for a panel opened by a chrome extension) but I needed to allow the user to change the window height, but prevent them changing the window width

@Derek's solution got me almost there but I had to tweak it to allow height changes and because of that, an endless resizing loop was possible so I needed to prevent that as well. This is my version of Dereck's answer that is working quite well for me:

var couponWindow = {
  width: $(window).width(),
  height: $(window).height(),
  resizing: false
};
var $w=$(window);
$w.resize(function() {
  if ($w.width() != couponWindow.width && !couponWindow.resizing) {
    couponWindow.resizing = true;
    window.resizeTo(couponWindow.width, $w.height());
  }
  couponWindow.resizing = false;
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!