Christina Z.
Christina Z.

Reputation: 1

Accessing parent window through Javascript (when child window's URL is on another server)

I understand that ajax can't go cross-server.

So I am trying to just access my parent window, like change its location. So it works when the child page is on the same server but not when it is on another server.

For example:

parenttest.html:

Click here

childtest.html

opener.document.location="http://www.cnn.com";

So this works when childtest is on the same server but not when it is on another server. Why can't I even access the opening window that popped up the pop-up window?

thanks

Upvotes: 0

Views: 2081

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92284

I couldn't find a way to run your code as you mentioned (server a opening a popup on server b, then having the popup change the location of opener). However, I tried the same thing with an iframe. Loaded google and tried to change it cnn.com. It allowed me when I used

window.frames.myframe.location = 'http://www.cnn.com'

It didn't work when I tried

window.frames.myframe.document.location = 'http://www.cnn.com'

Try removing document from your statement. Maybe there are a few things you can do to a window from a different domain. I know you can at least test if window.closed

Try it out: http://jsfiddle.net/HrWer/

New test

I started from wikipedia.com, typed into the URL box:

javascript: open('http://www.google.com');void(0)

From the window that opened, I typed the following statements into the URL box

javascript: alert(opener.closed) //false, it works

javascript: alert(opener.location.href) //undefined, access denied

javascript: opener.document.location = 'http://www.google.com'; void(0); // Didn't change the location, access denied

javascript: opener.location = 'http://www.google.com'; void(0); // YES, it changed

I think this almost confirms my theory, I only tested in chrome though.

Upvotes: 1

epascarello
epascarello

Reputation: 207521

Same origin policy is not going to let it happen.

Upvotes: 1

Related Questions