Disable Browser Window Resize
Asked Answered
R

4

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

Rhythmandblues answered 16/5, 2012 at 1:31 Comment(8)
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.Ileac
You should never try to change the behaviour of core OS level functionality (eg, preventing resize of a window). Ever.Bari
@JohnHartsock - Maybe he is making a browser extension or plugin?Artful
@Derek...If that is true then it would be wise to mention that in the question. Its not wise to assume. Ask the question first and get all the facts.Ileac
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.Rhythmandblues
@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.Bicentenary
@ChristianVarga Agreed, core behavior exists for a reason, and sometimes those reasons don't always apply or need to be changed. So what do you do? You create a wrapper, as you would suggest, and I don't disagree with that for this particular case. However, that's not everyone, nor is that every circumstance that they could be in. So don't misunderstand, I'm not saying "f the system," but merely pointing out that not every situation/circumstance is the same, and if they need to go about it in that way, they could. It's always good to follow the law, but sometimes it's necessary to break it.Bicentenary
For those who want to prevent resize of a window with a different origin: How to prevent resize and maximize of Javascript windowWalter
S
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.

Submarine answered 16/5, 2012 at 1:37 Comment(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.Rhythmandblues
@tchnchn - It should works, because I am using it in my extensions's popups. ;)Artful
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.Ube
Doesn't Google Chrome not allow you to modify the window size anymore?Leech
@Purmou - Why do you say that? If you are opening a window using window.open, then you can change the size of the window.Artful
@Derek: Exactly...I'm certain you can't modify the size of the current window after it's been opened.Leech
you cannot resize window if it has more tabs, you can only resize window opened by your script for whom you have handler.Scarrow
For me, this code seems to result in an endless loop, with window.resizeTo triggering the window(resize) handler and the handler executing window.resizeToBlackberry
O
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;
});
Otoscope answered 18/4, 2016 at 18:57 Comment(0)
A
0

It can be done with preventDefault

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

It can be done for some particular element, but keep everywhere else:

document.getElementById("my_element").addEventListener("wheel", (event) =>
{
   if (event.ctrlKey)
      event.preventDefault();
});
Anaclinal answered 1/2, 2023 at 13:13 Comment(0)
A
0

After struggling for about three hours trying to fix this exact problem, I came up with this hack:

let dims = [];
function getDims ()
{
  return [
    window.visualViewport?.width ||
      window.innerWidth ||
      document.documentElement.clientWidth ||
      document.body.clientWidth,
    window.visualViewport?.height ||
      window.innerHeight ||
      document.documentElement.clientHeight ||
      document.body.clientHeight
  ];
}

function fixViewport (e)
{
  const newdims = getDims();
  if (newdims[0] != dims[0])
  {
    dims = newdims;
    main.height = dims[1];
    main.style.height = dims[1] + "px";
  }
}
            
window.addEventListener("resize", fixViewport);
            
fixViewport();
body, main
{
  position: fixed;
  width: 100vw;
}

main { overflow-y: auto; }
<head>
  <meta id=viewportMeta name=viewport content="height=device-height, width=device-width, initial-scale=1.0">
</head>
<body>
  <main id=main>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at vehicula lectus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius leo sit amet felis egestas eleifend. Nulla efficitur magna libero, vitae facilisis libero consequat a. Praesent a justo sit amet mi viverra pulvinar. Pellentesque vehicula viverra nisl ut imperdiet. Sed at dignissim dolor. Maecenas sollicitudin purus nibh, non convallis mi mollis quis. Nulla facilisi. Nullam semper nisl nec eros ornare fermentum. Nunc nec tortor in turpis semper iaculis.</p>
  </main>
</body>

A few disclaimers:

  1. This code is far from production ready. It needs some extra work to get it there.
  2. This code is only tested on Android AppView, Chrome on Android, and Brave on Android. Kindly test it on your desired browser before use.
Apt answered 28/2 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.