Skip to content
Advertisement

how to resize a HTML browser window with javascript using Chrome

I have a program where I place the element using CSS in % of window height and width. When opened in a monitor with different resolution (only if the ratio height-width is different), the elements are a little bit misplaced. My solution is limit the height of the window when open in Chrome, so I can keep the same ratio. Researching in google, the command ResizeTo looks to be the answer and it looks simple, but I can’t make it to work. the command ResizeTo doesn’t work at all, what am I doing wrong?

HTML code:

<HTML>
<HEAD>
<TITLE>this is an HTML example</TITLE>
</HEAD>
<body style= "width:100%; height:100%; background-color:lightgrey;">
<H1>This is a Header</H1>
<H2>This is a Medium Header</H2>
<P> This is a new paragraph!
<P> <B>This is a new paragraph!</B>
<BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>
<script src="window.js" ></script>
</body>
</HTML>

Javascript:

window.resizeTo(1024,900)

Advertisement

Answer

In order to make window.resizeTo(x, y) to work, you need to have opened the window using window.open(…) with size dimentions. Moreover it is not acceptable to resize a browser window, hence Chrome and other browsers have disabled it by default. Quote from MDN

Note: It’s not possible to resize a window or tab that wasn’t created by window.open(). It’s also not possible to resize when the window has multiple tabs.

Advertisement