简体   繁体   English

JavaScript:将数据传递到后台的跨域弹出窗口

[英]JavaScript: Passing data to a cross-domain popup behind the scenes

The sites of my users collect referrer data about their users and store it in a cookie, which is bound to their domain. 我的用户的站点收集有关其用户的引荐来源网址数据,并将其存储在与他们的域绑定的Cookie中。 If the customer wants to initiate a chat and send the referrer data, they click a button which creates a popup with the URL being on MY domain (so I cannot access their cookies directly). 如果客户要发起聊天并发送引荐来源数据,则单击一个按钮,该按钮会创建一个弹出窗口,其URL在我的域中(因此我无法直接访问其cookie)。 I would like the popup window to receive the data stored in the cookie on their domain (assume I control the JS on their sites too). 我希望弹出窗口接收存储在其域中Cookie中的数据(假设我也控制了其站点上的JS)。

Ideally, I would do: 理想情况下,我会这样做:

var w = window.open(...);
w.originalReferrer = ...;

... but I hear this method of passing data to the popup only works if the popup is on the same domain (security restrictions). ...但是我听说这种将数据传递到弹出窗口的方法仅在弹出窗口位于同一域(安全性限制)时才有效。

I could also pass it as a GET arg: 我也可以将其作为GET参数传递:

window.open('chat?originalReferrer=' + encodeURIComponent(...) + ')

... but I'd prefer to keep the popup's URL clean, so no GET args should be visible. ...但是我希望保持弹出窗口的URL整洁,因此不应显示任何GET args。 Is there a way to clean it up, such as using a redirect (and since the destination is now on the same domain (my domain), there might be a nice JS way to pass this data)? 有没有办法清理它,例如使用重定向(并且由于目标现在位于相同的域(我的域)上,所以可能有一种不错的JS方式来传递此数据)?

Thanks :-) 谢谢 :-)

On their site, inject JS that will collect data from the cookie and put it in a hidden form that posts to your domain in a new window ( target="_blank" ). 在他们的网站上,注入将从cookie收集数据的JS并将其以隐藏的形式放入新窗口( target="_blank" )中发布到您的域中。

Posting to a new window isn't always going to give you a popup (tabs) so alternatively you could craft a popup in JavaScript (using var popup_window = window.open() , which gives you a reference to that window, and thus the document etc.). 发布到新窗口并不总是会为您提供一个弹出窗口(选项卡),因此您也可以在JavaScript中制作一个弹出窗口(使用var popup_window = window.open() ,该窗口为您提供了对该窗口的引用,因此文件等)。 Make the hidden form in the popup window and then post it to your domain. 在弹出窗口中创建隐藏的表单, 然后将其发布到您的域中。

The POST-ing is only necessary to keep your URLs clean, which is a good idea I think. POST-ing仅是保持URL干净所必需的,我认为这是个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM