user2995680 user2995680 - 1 year ago 74
HTML Question

resizeBy not working and don't know why

Why doesn't my resize button resize the window that my "create window" -button creates? I have searched and none of the answers I've found work. I'm all out of ideas now. Please help!

<div id="windowbuttons">
<button onclick="openWin()">Create window</button>
<button onclick="resizeWin()">Resize window</button>

<script>
var myWindow;

function openWin()
{
myWindow = window.open("http://www.google.com","", "width=100,height=100");
}

function resizeWin()
{
myWindow.resizeBy(250,250);
myWindow.focus();
}
</script>
</div>


And it does work correctly when I delete the url from window.open, so that it looks like

function openWin()
{
myWindow = window.open("","", "width=100,height=100");
}


What is going on?

Answer Source

var myWindow;

function openWin()
{
    myWindow = window.open("http://localhost","", "width=100,height=100");
}

function resizeWin()
{
    myWindow.resizeBy(250,250);
    myWindow.focus();
}
<div id="windowbuttons">
    <button onclick="openWin()">Create window</button>
    <button onclick="resizeWin()">Resize window</button>
</div>

You are opening http://google.com in a new window and then you are trying to resize http://google.com from your script, which is restricted. In order to work this, your child window must reside on the same host and port as of your parent window

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download