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

发布于 2024-11-07 02:55:25 字数 229 浏览 0 评论 0原文

我有两个域 X 和 Y,它们都位于具有不同 IP 的不同服务器上。

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

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

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

。我为此使用 PHP、HTML 和 js。

PS:当域名相同时,该方法有效,但我想要域名和服务器不同的解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疏忽 2024-11-14 02:55:25

我只是想补充一点,可以通过 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"
}

PREFIX 是可选的,但我更喜欢包含它,以防域 Y 被设置的其他页面访问window.name 属性。另请注意,您不需要使用 JSON(如果您使用的是恐龙浏览器,则不应该使用 JSON),但我喜欢 JSON,因为我可以在一个对象中传递多个属性。

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

在域 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";
}

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);
}

在原始页面中,应该有一个名为 processData 的函数,它获取数据并对其执行某些操作。

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.

甩你一脸翔 2024-11-14 02:55:25

您需要调查

CORS(对于较旧的IE,您将需要XDR) 或

窗口消息传送

JSONP

通过 url 发送变量

You need to investigate

CORS (for older IEs you will need XDR) or

window messaging or

JSONP or

send the variables via the url

‖放下 2024-11-14 02:55:25

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

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

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

...位于 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();
}

更多信息位于 Mozilla
致谢@mplungjan

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文