Passing Data from Parent Window of One domain to Child window of another domian
I have two domains say X and Y, both are on different server with different IPs.
Now the case is that on one page of domain X there is a Link which opens the pop-up of domain Y.
User searches for some data on that popup and then clicks on "Done"
On click the values related to the searched field should be passed to a page on domain X.
I开发者_Go百科 am using PHP, HTML, and js for this.
P.S.: The thing works when the domain name is same but I want the solution where domain names and server are different.
I just want to add that it is possible to pass data from a window with one domain to a window with another domain via the window.name
property. Of course, this property wasn't intended for that purpose, and language purists are going to hate me for this. Nonetheless, this is how it's done, quick and dirty:
On Domain X:
var PREFIX = "your prefix here";
// The second parameter of window.open() sets window.name of the child window.
// Encode JSON and prepend prefix.
window.open("http://domain-y.example.com/", PREFIX + JSON.stringify({"foo":"bar", "abc":123}));
On Domain Y:
var PREFIX = "your prefix here";
if(window.name.substr(0, PREFIX.length) == PREFIX){
// Remove prefix and decode JSON
var data = JSON.parse(window.name.substring(PREFIX.length));
// Do what you need to do with the data here.
alert(data.foo); // Should alert "bar"
}
The PREFIX
is optional, but I prefer to include it in case Domain Y is accessed by some other page that sets the window.name
property. Also note that you're not required to use JSON (and that you shouldn't if you're dealing with dinosaur browsers), but I like JSON because I can pass more than one property in an object.
EDIT: If you need Domain Y to pass data back to Domain X, you can have Domain Y save data in window.name
and navigate to a passer page on Domain X, which can easily pass data to the original window. Try this:
On Domain Y:
// Somewhere earlier in the code:
var PREFIX = "your prefix here";
// Call this function when the Done button is clicked.
function passDataBack(data){
window.name = PREFIX + JSON.stringify(data);
window.location = "http://www.domain-x.com/passer.html";
}
On http://www.domain-x.com/passer.html:
// Somewhere earlier in the code:
var PREFIX = "your prefix here";
if(window.name.substr(0, PREFIX.length) == PREFIX){
// Remove prefix and decode JSON
var data = JSON.parse(window.name.substring(PREFIX.length));
// Send data to parent window
window.opener.processData(data);
}
In the original page, there should be a function called processData
that takes the data and does something with it.
You need to investigate
CORS (for older IEs you will need XDR) or
window messaging or
JSONP or
send the variables via the url
I know this is an old question but I think this may be a more appropriate answer to the question
You should add the following code to the http://domain-x.com
window.addEventListener("message", function(event) {
console.log(event.data); // {user: 'data'}
}, false);
... at the http://domain-y.com
userClicksDone() {
try {
// This may throw an error in case of people access
// http://domain-y.com directly, not via popup from
// http://domain-x.com
//
window.opener.postMessage({user: 'data'}, 'http://domain-x.com');
} catch(e) { }
// Closes this popup
//
window.close();
}
More info at Mozilla. Credits to @mplungjan
精彩评论