Cross-domain JavaScript code with sibling sub-domains - Stack Overflow

admin2025-04-19  0

I have two web applications setups:

david.example and john.example

David opens a new window to John:

window.open('john.example');

John would now like to set an input element back on David:

$("#input", window.opener.document).val("Hello David.");

My problem is that this doesn't work on most modern browser because of cross-domain scripting security. I've also tried setting document.domain to different domain binations both on David and John with no success.

The only time this does work is if John is on example and both have document.domain set to example. However, example is our main website and is not available as a solution.

So is there a way I can do the above example while making the solution works in all modern security conscious browsers?

Notes

  • I control example and all its sub domains.
  • David and John are separate web applications and cannot be hosted on the same sub-domain.

I have two web applications setups:

david.example. and john.example.

David opens a new window to John:

window.open('john.example.');

John would now like to set an input element back on David:

$("#input", window.opener.document).val("Hello David.");

My problem is that this doesn't work on most modern browser because of cross-domain scripting security. I've also tried setting document.domain to different domain binations both on David and John with no success.

The only time this does work is if John is on example. and both have document.domain set to example.. However, example. is our main website and is not available as a solution.

So is there a way I can do the above example while making the solution works in all modern security conscious browsers?

Notes

  • I control example. and all its sub domains.
  • David and John are separate web applications and cannot be hosted on the same sub-domain.
Share edited Apr 6, 2011 at 12:42 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Mar 3, 2011 at 16:40 GateKillerGateKiller 76.1k75 gold badges175 silver badges204 bronze badges 1
  • What do you mean with "However, example. is our main website and is not available as a solution."? Setting document.domain to "example." is the right solution. – RoToRa Commented Mar 3, 2011 at 16:48
Add a ment  | 

2 Answers 2

Reset to default 8

You should be able to do this, as long as you set document.domain on both DOM's.

document.domain = location.host.replace(/^.*?([^.]+\.[^.]+)$/g,'$1');

Credited to Martin Jespersen

You can always use iframes for cross domain munication and send messages via the hashbangs. See this for an example. It's basically the workings of stuff like Facebook connect. If one app opens the other window I'm guessing you can do that with normal windows as well.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745072072a283342.html

最新回复(0)