iframe解决跨域问题(跨域修改iframe页面内容详解)
iframe解决跨域问题
跨域修改iframe页面内容详解原理
主站点内嵌代理页面, 并向代理页传递数据, 代理页根据主站点的数据对目标页的DOM进行操作.由于代理页与目标页同域, 所以代理页可以获取并操作目标页的document对象.
前提条件
需要将proxy.html放到与内嵌的iframe页同域的服务下, 并且可以被访问到.
使用
支持2种调用方式: 使用 postMessage 和 URL params.
postMessage
该方法需要使用 JSON.stri.jpg" alt="iframe解决跨域问题(跨域修改iframe页面内容详解)" border="0" />
URL params 该方法需要将传递的内容用 encodeURIComponent 编码.
// React
function IframeProxy(props) {
handleLoad = (e) => {
e.target.contentWindow.postMessage(JSON.stri.jpg" alt="iframe解决跨域问题(跨域修改iframe页面内容详解)" border="0" />
// React
function IframeProxy(props) {
var params = 'iframe=' + encodeURIComponent(`
<iframe name="target" title="target" className="target" src="http://www.targetdomain.com/target.html" frameBorder="0" scrolling="no" style="width: 100%;height:100%"></iframe>
`);
params += '&includeStyle=' + encodeURIComponent(`
body {
background-color: red;
}
header {
display: none;
}
footer {
display: none;
}
`);
params += '&includeScript=' + encodeURIComponent(`
window.addEventListener('load', function(event) {
alert(document.querySelector('body').innerHTML);
});
`);
params += '&importStyle=' + encodeURIComponent(`
http://www.mydomain.com/assets/css/import.css
`);
params += '&importScript=' + encodeURIComponent(`
http://www.mydomain.com/assets/js/import.js
`);
return <iframe name="proxy" title="proxy" className="proxy" width="100%" height="100%" src={`http://www.targetdomain.com/proxy.html?${params}`} frameBorder="0" scrolling="no"></iframe>;
}
API
<iframe src="http://www.targetdomain.com/proxy.html?params"></iframe>; params: { origin: 当前站点的域名, 使用postMessage方式时必填, proxy用来校验发出消息的源域名. iframe: 需要内嵌的iframe标签字符串, includeStyle: 希望添加到iframe页的css内容, includeScript: 希望添加到iframe页的js内容, importStyle: 希望引入到iframe页的css资源链接, 如果目标站点使用安全协议(https), 资源链接使用非安全协议(http), 该功能会被浏览器禁止. importScript: 希望引入到iframe页的js资源链接, 如果目标站点使用安全协议(https), 资源链接使用非安全协议(http), 该功能会被浏览器禁止. }
注意: 处于安全问题, 默认禁用了 includeScript 和 importScript 功能, 如需启用在proxy.html中将变量 ENABLED_JS_INCLUDE 设置为 true 即可.
资源
https://github.com/stephenliu1944/cross-domain-iframe-proxy
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
- Flask框架踩坑之ajax跨域请求实现(Flask框架踩坑之ajax跨域请求实现)
- nginx跨域怎么用(如何利用map实现Nginx允许多个域名跨域)
- 浏览器为什么有跨域机制(详解window.open被浏览器拦截的解决方案)
- vue和springboot分页开发实现(解决Vue+SpringBoot+Shiro跨域问题)
- thinkphp5.1修改(ThinkPHP 5.1 跨域配置方法)
- iframe嵌入页面跨域(使用iframe+postMessage实现页面跨域通信的示例代码)
- html5 postMessage(html5关于外链嵌入页面通信问题postMessage解决跨域通信)
- laravel对请求参数解密(Laravel开启跨域请求的方法)
- nginx前后端跨域(Nginx解决前端访问资源跨域问题的方法详解)
- laravel设置跨域请求(解决laravel 出现ajax请求419unknown status的问题)
- laravel设置跨域请求(Laravel实现ApiToken认证请求)
- iframe跨域原理(详解使用postMessage解决iframe跨域通信问题)
- iis配置网站授权(IIS的web.config中跨域访问设置方法)
- iframe如何解决跨域问题(关于iframe跨域使用postMessage的实现)
- iframe跨域获取标签(iframe跨域的几种常用方法)
- axios如何跨域请求(axios取消请求与避免重复请求)
- 冰岛旅游攻略(冰岛旅游攻略及花费)
- 为什么现在年轻人越来越喜欢买衣服(为什么现在年轻人越来越喜欢买衣服穿)
- 怎么做好SEO(怎么做好seo内容优化)
- 冬季钓鱼子线用 长 还是 短(冬季钓鱼子线用)
- 鱼竿 夏钓短,冬钓长 ,一定是这样 认清优缺点在选竿(鱼竿夏钓短冬钓长)
- 鲢鳙钓底还是钓浮 流水的水域应怎样做钓(鲢鳙钓底还是钓浮)
热门推荐
- lnmp 自动化安装(LNMP系列教程之 设置301重定向的方法)
- python爬虫request方法介绍(详解Python3网络爬虫二:利用urllib.urlopen向有道翻译发送数据获得翻译结果)
- windows 2003服务器系统安装(win2003云服务器安装西部数码网站助手教程)
- python语句for循环(Python基础之循环语句用法示例for、while循环)
- ftp命令详解(FTP 常用命令 使用说明)
- js中Math对象的用法
- dedecms 怎么设置栏目(dedecms如何实现tag标签伪静态的方法)
- python程序运行步骤(详解python运行三种方式)
- python将网站图片本地存储(详解Python下载图片并保存本地的两种方式)
- mysql binlog日志位置(开启MySQL的binlog日志的方法步骤)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9