Skip to content
Advertisement

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:

http://thedailynathan.com/files/outlink/parent/parent.html

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

Advertisement

Answer

Change the:

document.getElementById("myframe").contentWindow.moveMe()

to:

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

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement