Javascript 将数据从一个域的父窗口传递到另一个域的子窗口

Javascript 将数据从一个域的父窗口传递到另一个域的子窗口,javascript,html,webforms,parent-child,Javascript,Html,Webforms,Parent Child,我有两个域名,比如X和Y,它们都在不同的服务器上,具有不同的IP 现在的情况是,在域X的一个页面上有一个链接打开域Y的弹出窗口 用户在弹出窗口中搜索一些数据,然后单击“完成” 单击时,与搜索字段相关的值应传递到域X上的页面 为此,我使用PHP、HTML和js 注意:当域名相同时,这一点是可行的,但我希望在域名和服务器不同的地方找到解决方案。您需要调查 (对于您需要的较老的IEs)或 或 或 通过url发送变量我只想补充一点,可以通过window.name属性将数据从一个域的窗口传递到另一个域的窗

我有两个域名,比如X和Y,它们都在不同的服务器上,具有不同的IP

现在的情况是,在域X的一个页面上有一个链接打开域Y的弹出窗口

用户在弹出窗口中搜索一些数据,然后单击“完成”

单击时,与搜索字段相关的值应传递到域X上的页面

为此,我使用PHP、HTML和js


注意:当域名相同时,这一点是可行的,但我希望在域名和服务器不同的地方找到解决方案。

您需要调查

(对于您需要的较老的IEs)或


通过url发送变量

我只想补充一点,可以通过
window.name
属性将数据从一个域的窗口传递到另一个域的窗口。当然,这个属性并不是为了达到这个目的,而且语言纯粹主义者会因此讨厌我。尽管如此,这是如何做到的,既快又脏:

在域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}));
在域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"
}
// 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";
}
前缀
是可选的,但我更愿意将其包含在设置
window.name
属性的其他页面访问域Y的情况下。还请注意,您不需要使用JSON(如果您使用的是恐龙浏览器,则不应该使用JSON),但我喜欢JSON,因为我可以在一个对象中传递多个属性

编辑:如果您需要域Y将数据传递回域X,您可以让域Y在
窗口中保存数据。name
并导航到域X上的passer页面,该页面可以轻松地将数据传递到原始窗口。试试这个:

在域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"
}
// 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";
}
关于:


在原始页面中,应该有一个名为
processData
的函数,它获取数据并对其进行处理。

我知道这是一个老问题,但我认为这可能是对该问题的更合适的答案

您应该将以下代码添加到
http://domain-x.com

window.addEventListener("message", function(event) {
  console.log(event.data); // {user: 'data'}
}, false);
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();
}
。。。在
http://domain-y.com

window.addEventListener("message", function(event) {
  console.log(event.data); // {user: 'data'}
}, false);
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();
}
更多信息请访问。
@mplungjan的功劳

我正在阅读有关它的文章,尽管演示代码非常有用,到处都有演示。首先计算您要使用的技术。很抱歉,我没有时间粘贴4个示例或从头开始编写一些示例,因为我需要两个域/子域上的代码。如果你有两个域上的代码的完全访问权限,请尝试CORS。好的,谢谢。我将尝试CORS,并检查它是否可以解决我的问题。CORS非常简单。您只需要在服务器B上返回一些就可以允许服务器A访问内容。我使用它在生产中没有任何问题。