14

For starters... I have no sinister intention of subjecting users to popups or anything like that. I simply want to prevent a user from resizing the browser window of a webpage to which they've already navigated (meaning I don't have access to / don't want to use window.open();). I've been researching this for quite a while and can't seem to find a straightforward answer.

I felt like I was on track with something along the lines of:

 $(window).resize(function() {
     var wWidth = window.width,
     wHeight = window.height;
     window.resizeBy(wWidth, wHeight);
 });

...to no avail. I have to imagine this is possible. Is it? If so, I would definitely appreciate the help.

Thanks

8
  • 5
    I must ask why? The browser window is not suppose to be restricted. If the end-user wants to move it to another screen and cannot resize it, this could pose potential issues to the end user. Example is two screens with different resolutions. May 16, 2012 at 1:36
  • 4
    You should never try to change the behaviour of core OS level functionality (eg, preventing resize of a window). Ever.
    – Christian
    May 16, 2012 at 1:38
  • 1
    @JohnHartsock - Maybe he is making a browser extension or plugin? May 16, 2012 at 1:38
  • 2
    Unfortunately, I'm bound by NDA's on this one but @Derek is on the right track. I know this goes against everything we all believe, which is why I hesitated to disturb the community with this question. The short answer regarding justification is that this website will only be accessed under specific (and other controlled) conditions. The user knows what to expect upon navigation to it. Thx everyone.
    – Matt Rose
    May 16, 2012 at 1:53
  • 6
    @ChristianVarga rofl....You should never assume such things, ever. There are many situations/circumstances that one would potentially need to do something along these lines. Maybe this guy is creating a site in a controlled environment (such as at work, etc..), rather than just some random public internet website. For most websites? You're probably right. For every possible scenario conceivable? You're wrong.
    – Fata1Err0r
    May 26, 2015 at 18:56

3 Answers 3

27

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.

8
  • Thanks @Derek. This is definitely along the lines of what I was thinking. I've plugged in your suggestion and I'm still not having success. However, just by looking at what you've written, I can see that this should work. This is still helpful to me because it confirms that I must be doing something wrong somewhere else... which is why I'm going to mark this as 'answered'. Thx.
    – Matt Rose
    May 16, 2012 at 1:56
  • @tchnchn - It should works, because I am using it in my extensions's popups. ;) May 16, 2012 at 1:59
  • 1
    That is a very annoying "feature". If it works (and it won't, given the OP) the user is locked into whatever size the window initially opens, which might be useful, but probably isn't.
    – RobG
    May 16, 2012 at 2:06
  • 1
    Doesn't Google Chrome not allow you to modify the window size anymore?
    – Purag
    May 16, 2012 at 4:39
  • 1
    @Purmou - Why do you say that? If you are opening a window using window.open, then you can change the size of the window. May 16, 2012 at 5:08
2

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

If need some particular element to handle resize in some particular mode, and prevent whole window from resizing use preventDefault

document.getElementById("my_element").addEventListener("wheel", (event) =>
{
   if (event.ctrlKey)
      event.preventDefault();
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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