How to prevent resize and maximize of Javascript window

html, javascript

Solution

It is completely possible

... albeit limited

By listening for the window `resize` event and using the `Window.resizeTo()` function, it is possible to prevent the window from resizing.

var x = window.open("https://www.stackoverflow.com", "_blank", "toolbar=no,menubar=no,scrollbars=yes,dialog=yes,resizable=no,top=100,left=250,width=800,height=530");
x.addEventListener("resize", () => {
    x.resizeTo(800, 530);
})

This is not a cross-origin solution - you cannot add event listeners etc,. on a window with a different origin as it can be used for malicious purposes.

Result:

[Try it yourself]

Supporting different-origin

The following solution creates a new window using the current URL, then overwrites the document with an `<iframe>` containing the source of the desired site.

why do we first open using the current URL? So we can manipulate the DOM without being annoyed with these "Blocked a frame with origin "..." from accessing a cross-origin frame" errors, and also so that we can attach the resize event listener to the window (not possible with the original solution).

var goto = "https://wix.com";
var x = window.open(location, "_blank", "toolbar=no,menubar=no,scrollbars=yes,dialog=yes,resizable=no,top=100,left=250,width=800,height=530");
x.document.write(`<html><head><style>body{margin:0;overflow:hidden;}iframe{width:100%;height:100%;border:0}</style></head><body><iframe src="${goto}"></iframe></body></html>`);
x.addEventListener("resize", () => {
    x.resizeTo(800, 530);
})

Result:

[Try it yourself]

Which works on most sites. But you'll come across a "<sitename> refused to connect" once in a while.

If you want to know why, check out: iframe refuses to display

As for preventing maximization of the window, there does not seem to be any way to minimize it programmatically note: the `resize` event is fired during maximization. `Window.minimize()` looks promising, but as of yet there is no support for it across any browser.

Problem

How do I prevent resizing and maximizing of the Javascript window? I am using the following code: ``` var win = window.open(myURL, "_blank", "toolbar=no,menubar=no,scrollbars=yes,dialog=yes,resizable=no,top=100,left=250,width=800,height=530"); ``` Have tried multiple solutions from Stackoverflow but had no luck. Can anybody help me?

Original source

Related problems