javascript - 像 Optimizely 一样访问跨域 iframe 元素

标签 javascript iframe optimizely

我希望能够在前端使用 javascript 从跨域 iframe 中手动选择文本。

我知道要达到这样的结果是相当困难的,因为如果我决定使用 100% javascript 解决方案,则需要使用 postMessage。我对后端解决方案持开放态度(php 或 nodejs)

Optimizely 可以从页面访问任何 dom 元素,我想了解他们是如何做到的。是因为用户需要先在他的网站上安装一个小部件吗?如果是,它到底是如何工作的?

这是一个例子: example

最佳答案

转载自#2 of this answer :

Let's say you want to create an experiment on http://www.mypage.com ... The Optimizely Editor loads http://www.mypage.com inside an iframe and uses window.postMessage to communicate with the page. This only works if that page already has a snippet like [<script src="//cdn.optimizely.com/js/XXXXXX.js"></script>] on it. If that's not the case, the editor will timeout while waiting for a message from the iframe'd page, and will load it again via a proxy that actually inserts the snippet onto the page. This loading process allows the editor to work with pages that a. contain an account snippet b. do not contain an account snippet, or c. sit behind a firewall (c. requires the snippet.)

关于javascript - 像 Optimizely 一样访问跨域 iframe 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55625620/

相关文章:

javascript - 如何在laravel中调用 ' '标记的路线

javascript - Ajax 成功返回值在其他函数中的使用

javascript - 使用 Bacon.js 消除唯一值的抖动

javascript - 将整个网站放在主登录页面的 iframe 中有什么缺点?

javascript - Adobe Acrobat 9 Javascript 将页脚添加到文档

java - 文本选择在 vaadin 应用程序内的 iframe 中不起作用

html - 将 XML 文件采购到 DIV 和 iframe 时不采用 CSS 属性

javascript - 实验报告中没有流量

jquery - 优化淡入淡出

google-analytics - 将GTM脚本放在head部分有什么负面后果?