0

I have a window I am opening via window.open. The call to window.open can take some parameters like width, height, scroll bars, etc. In my application, I would like to use window.open to show a screen of varying height. For instance, it may be 200 pixels high or 400 pixels high depending on the user state. The state is unknown at the location of window.open. Is it possible to have the opened window be sized to fit its content? As far as I can tell, you can use window.resizeTo, but this doesn't seem to work in Chrome.

Max
  • 15,157
  • 17
  • 82
  • 127
  • What if you use [the current size of the screen?](http://stackoverflow.com/questions/3437786/get-the-size-of-the-screen-current-web-page-and-browser-window) – Mike Cluck Jun 01 '16 at 19:30
  • How will that solve the issue? – Max Jun 01 '16 at 19:33
  • 1
    Oh, sorry, I misread that. I thought you meant fit to the screen, not the size of the yet to be loaded contents. Given that information, you can't really do that since you can't know the total size of the contents before the page loads. Especially when you consider that a user could have a global zoom setting. – Mike Cluck Jun 01 '16 at 19:34
  • Are you storing the reference to your open window and then calling [resizeTo](https://developer.mozilla.org/en-US/docs/Web/API/Window/resizeTo)? `var newWindow = window.open("","lorem","height=200,width=200");newWindow.resizeTo(200,400)` –  Jun 01 '16 at 19:50
  • if the state is known at window.opener (ie. the parent window) then maybe you can render the content in there in an isolated element to figure out the needed height – yezzz Jun 01 '16 at 20:01
  • @Austin I was calling `window.resizeTo` inside the child window. In Chrome the window size was not set correctly. – Max Jun 01 '16 at 21:11

1 Answers1

0

If possible, load the content in the parent page inside a container div, obtain the container div's width and height using Javascript's *.offsetWidth and *.offsetHeight, then use those values in window.open to have the new window fit the content.

user3163495
  • 2,425
  • 2
  • 26
  • 43