JavaScript实现iframe自动高度调整和不同主域名跨域
作者:常思过 发布时间:2024-04-16 09:47:33
标签:JavaScript,iframe
大家都知道Js有同源策略,就是主域名不同嵌套的iframe不允许Js通信的。
比如我有一个网站,网站中要嵌入其网站的页面。那么我可以使用iframe引用第三方网站的地址即可。
但是问题也来的iframe的高度是固定的不能与第三方网站很好的融合,又例如第三方网站使用了瀑布流插件,要滚动加载自动计算高度,那么先说跨域:iframe主域名不同跨域方法,假如网站 A.com B.com A 里面放入一个iframe 引用了B.com,这种情况下B.com里面的Js是无法访问到A.com的。JS跨域必须是同源,就是同一个主域名,那肿么办呢?
我们可以在B.com中在引入一个iframe,暂且叫C吧 这个iframe加载A.com中的一个网页。这样同源就有了B.com中的iframe中的网页就可以和A.com通讯了。下面只要B和C通讯,让C和A通讯就完成B->A通讯,这样当B高度变化时通知C,让C通知A把iframe高度调整。
B和C通讯,其实通过url地址就可以实现通讯了,B.com iframe设置为隐藏,改变src地址时候C就可以接收。
废话不说了上代码
A.com/index.html
<html>
<script src="{$smarty.const.STATIC_URL}/js/jquery-1.7.1.min.js"></script>
<script>
var test = function() {
$('#h1').html('test');
}
</script>
<body>
<h1 id="h1">nba.alltosun.net</h1>
<iframe id="ifm" width="760" height="100" src="http://***.sinaapp.com/"></iframe>
</body>
</html>
B.com/index.html
<html>
<head></head>
<body>
<h1>**.appsina.com</h1>
<button id="button">设置高度</button>
<div id="div" style="height:200px;display:none;"></div>
<script src="http://nba.alltosun.net/js/jquery-1.7.1.min.js"></script>
<script>
$(function(){
window.ifrH = function(e){
var searchUrl = window.location.search;
var b = null;
var getParam = function(url, param) {
var q = url.match(new RegExp( param + "=[^&]*")),
n = "";
if (q && q.length) {
n = q[0].replace(param + "=", "");
}
return n;
}
var f = getParam(searchUrl,"url"),
h = getParam(searchUrl, "ifmID"),
k = getParam(searchUrl, "cbn"),
g = getParam(searchUrl, "mh");
var iframeId = 'testiframe';
var iframe = document.getElementById(iframeId);
var divId = 'alltosun';
if (!iframe){
var iframe = document.createElement('iframe');
iframe.id = iframeId;
iframe.style.display = "none";
iframe.width = 0;
iframe.height = 0;
document.body.appendChild(iframe);
}
if (e && e.type == "onload" && h) {
b.parentNode.removeChild(b);
b = null;
}
if (!b) {
b = document.createElement("div");
b.id = divId;
b.style.cssText = "clear:both;"
document.body.appendChild(b);
}
var l = b.offsetTop + b.offsetHeight;
iframe.src = (decodeURIComponent(f) ||
"http://*****/test2") + "&h=" + l + "&ifmID=" + (h || "ifm") + "&cbn=test" + "&mh=" + g + "&t=" + ( (+ new Date()));
if (e && e.type =="onload") {
window.onload = null;
}
}
window.onload = function() {
ifrH({type: "onload"});
}
// ifrH();
$('button').click(function(){
$('div').show();
ifrH();
})
})
</script>
</body>
</html>
C 代理文件
<script>
var search = window.location.search,
getSearchParam = function (search, key) {
var mc = search.match (new RegExp ( key + "=([^\&]*)") ),
ret="";
mc && mc.length && (ret = mc[0].replace( key + "=",""));
return ret;
},
// 参数h
h = getSearchParam(search,"h"),
ifmID = getSearchParam(search,"ifmID"),
cbn = getSearchParam(search,"cbn"),
// 宽高
mh = getSearchParam(search,"mh") || h,
isFunction = function(fn){
return !!fn && !fn.nodeName && fn.constructor != String
&& fn.constructor != RegExp && fn.constructor != Array
&& (/function/i).test(fn + "");
};
try{
if(parent && parent.parent){
ifm = parent.parent.document.getElementById(ifmID);
ifm && mh && (ifm.height=mh);
fn=parent.parent[cbn];
isFunction(fn) && fn.call();
ifm=null;
}
}catch(ex){
console.log(ex);
}
</script>


猜你喜欢
- pyfinance简介datasets.py :金融数据下载(基于request进行数据爬虫,有些数据由于外网受限已经无法下载);gener
- 多对多中间表详解我们都知道对于ManyToMany字段,Django采用的是第三张中间表的方式。通过这第三张表,来关联ManyToMany的
- 介绍Matplotlib 可能是 Python 2D-绘图领域使用最广泛的套件。它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式。
- 参考 https://books.agiliq.com/projects/django-admin-cookbook/en/lat
- 目前lnmp一键安装包比较多的问题就是502 Bad Gateway,大部分情况下原因是在安装php前,脚本中某些lib包可能没有安装上,造
- 开发中经常会使用npm install 安装依赖包,经常会看到^符号和~符号,现将二者的区别总结如下:版本号 x.y.z : z
- 记录一次小白的tensorflow学习过程,也为有同样困扰的小白留下点经验。先说我出错和解决的过程。在做风格迁移实验时,使用预加载权重的VG
- bookheader.asp Recommended Books for <%=session(&quo
- JSP之表单提交get和post的详解及实例一 get和post的区别二 实战(post方式提交)1、login.jsp<%@ pag
- 包括安装时提示有挂起的操作、收缩数据库、压缩数据库、转移数据库给新用户以已存在用户权限、检查备份集、修复数据库等 (一)挂起操作 在安装Sq
- 它可和PHP、JavaScript、ASP、ASP.NET、ColdFusion、Java、以及ABAP等不同的编程语言相结合。FCK的配置
- 跟着趣味开发python一起实现的弹球小游戏游戏运行效果实现流程1.创建游戏画布(创建ball类)2.增加几个动作(让小球移动、让小球来回反
- 最近尝试把项目迁移到Python环境下,特别新装了一台干净的Debian系统,准备重新配置环境,上网找了一些运行Python Web的环境方
- <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC &qu
- 前言:远程连接中兴设备(系统使用的中兴网卡)时使用的事Telnet连接,连接时设有二次验证,每次输入用户名密码和执行命令是个繁琐的过程,使用
- 1. 多曲线1.1 使用pyplot方式import numpy as npimport matplotlib.pyplot as pltx
- 本文实例讲述了Python求两个文本文件以行为单位的交集、并集与差集的方法。分享给大家供大家参考。具体实现方法如下:s1 = set(ope
- 4 月 27 日,GitHub 趋势榜第 3 位是一个用 Python 编码实现的算法库,Star 数早已达到 26000+链接:https
- 前言在日常开发中,用mysql进行查询的时候,有一个比较少见的关键词exists,我们今天来学习了解一下这个exists这个sql关键词的用
- MySQL中concat函数使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串。如有任何一个参数为N