Skip to content

Redirecting child iframe to relative url, using js within iframe – but url is relative to the parent

Some interesting behavior that I didn’t expect. I have a page which contains an iframe, and in that iframe there’s a javascript function that redirects its own window.

I call the iframe’s js function from the parent frame. My expected behavior is that it will redirect the iframe to a new page, relative to the iframe’s existing location.

Instead, it gets redirected relative to the parent frame’s location.

You can see a demo here:

Am I doing something wrong here, or will I just have to code in an absolute url for my redirect?

Found this thread that sounds very similar. However no one came up iwth an answer for it:

Using relative url for window.location in child iframe


Change the:



document.getElementById("myframe").contentWindow.location = "javascript:moveMe()"

This way, the moveMe executed in the iframe’s context.