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

Abh*_*hvi 2 html javascript webforms parent-child

我有两个域名X和Y,两个域都在不同的IP服务器上.

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

用户在该弹出窗口中搜索某些数据,然后单击"完成"

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

我正在使用PHP,HTML和js.

PS:当域名相同但事我想要域名和服务器不同的解决方案时,这件事有效.

wec*_*sam 7

我只想补充说,可以通过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}));
Run Code Online (Sandbox Code Playgroud)

在域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"
}
Run Code Online (Sandbox Code Playgroud)

PREFIX是可选的,但我更喜欢包含它,以防域名Y由设置window.name属性的其他页面访问.另请注意,您不需要使用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";
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

在原始页面中,应该有一个调用函数processData来获取数据并对其执行某些操作.

  • 这是一个很棒的黑客.我在生产中没有任何问题地使用它. (2认同)