来自父级的 iFrame 点击监控
我们有一位客户需要我们通过 iFrame 在他们的网站中包含一个工具。在 iFrame 中,我们显示产品,当单击“添加到购物车”链接时,需要将产品添加到父文档购物车。父级和子级不在同一域中。不幸的是,客户端无法为我们提供发布到购物车的方法,我们所能做的就是调用他们的 cart.add(qty,id)
函数,但它无法在iFrame 因为iFrame 显然没有可用的功能。此外,我们不能只是伪造帖子到购物车,因为我们无法访问会话 ID。幸运的是,我可以将我需要的任何 Javascript 添加到他们的主页上。
有没有办法让我监听 iFrame 中来自父级的点击,然后从 iframe 中提取数量和产品 id 值,然后执行 cart.add
函数?考虑到限制,是否有更好的方法来解决这个问题?
We have a client who needs us to include a tool in their site via iFrame. Within the iFrame, we are displaying products and when the "add to cart" link is clicked, it needs to add the product to the parent documents cart. The parent and child are not on the same domain. Unfortunately, the client can't provide us with a method to post to the cart and all we can do is call their cart.add(qty,id)
function, but it won't work within the iFrame because the iFrame obviously doesn't have the function available. In addition, we can't just fake the post to cart as we can't access the session id. Luckily I can add whatever Javascript I need to their main page.
Is there a way for me to listen for clicks in the iFrame from the parents, then to pull the quantity and product id values from within the iframe, then execute the cart.add
function? Is there a better way to approach this given the constraints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是不可能的,因为它存在安全风险,并且被同源策略阻止。
This isn't possible as it's a security risk and is prevented by the same-origin policy.
不要使用 iFrame。使用 jquery .load() 将整个其他站点放置在 div 中。确保您的“点击”是 live() ,以便它们捕获对添加内容的点击,应该没问题。我不知道您加载的网站有多大。另外,通过加载,您可以仅指定页面的一部分来引入,或者如果您不需要所有标题信息,则可以指定一个特定的 div。
dont use an iFrame. use jquery .load() to place the whole other site within a div. Make sure your "clicks" are live() so they catch clicks on the added content and it should be fine. I dunno how huge the site your loading in is. Also with load you can specify just a chunk of the page to bring in so just or a specific div if you don't need all the header info.
我想出了一个方法。我写了一篇文章在这里解释:http://blog.igodigital.com/blog/notes-on-cyber-weekend-targeted-email-campaigns/enabling-cross-domain-js-with-iframes -有趣的方式
I figured out a method. I wrote a post to explain here: http://blog.igodigital.com/blog/notes-on-cyber-weekend-targeted-email-campaigns/enabling-cross-domain-js-with-iframes-the-fun-way