最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing Data from Parent Window of One domain to Child window of another domian - Stack Overflow

programmeradmin2浏览0评论

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 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 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 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.

Share Improve this question asked May 16, 2011 at 5:56 Abhishek SanghviAbhishek Sanghvi 4,6516 gold badges29 silver badges36 bronze badges 1
  • Check out this JSONP example: ibm./developerworks/library/wa-aj-jsonp1 – wdm Commented May 16, 2011 at 6:36
Add a ment  | 

3 Answers 3

Reset to default 7

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./", 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./passer.html";
}

On http://www.domain-x./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.

window.addEventListener("message", function(event) {
  console.log(event.data); // {user: 'data'}
}, false);

... at the http://domain-y.

userClicksDone() {
  try {
    // This may throw an error in case of people access
    // http://domain-y. directly, not via popup from
    // http://domain-x.
    //
    window.opener.postMessage({user: 'data'}, 'http://domain-x.');
  } catch(e) { }

  // Closes this popup
  //
  window.close();
}

More info at Mozilla. Credits to @mplungjan

发布评论

评论列表(0)

  1. 暂无评论