`
dj4kobe
  • 浏览: 20558 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

[转]利用location.hash实现跨域iframe自适应高宽

阅读更多
觉得写得不错,以前工作的时候也想过做frame的自适应高度的,可惜没做成。现在终于学到了。

页面域关系:
主页面a.html所属域A:www.taobao.com
被iframe的页面b.html所属域B:www.alimama.com,假设地址:http://www.alimama.com/b.html
实现效果:
A域名下的页面a.html中通过iframe嵌入B域名下的页面b.html,由于b.html的宽度和高度是不可预知而且会变化的,所以需要a.html中的iframe自适应大小.
问题本质:
js对跨域iframe访问问题,因为要控制a.html中iframe的高度和宽度就必须首先读取得到b.html的大小,A、B不属于同一个域,浏览器为了安全性考虑,使js跨域访问受限,读取不到b.html的高度和宽度.
解决方案:
引入代理代理页面c.html与a.html所属相同域A,c.html是A域下提供好的中间代理页面,假设c.html的地址:www.taobao.com/c.html,它负责读取location.hash里面的width和height的值,然后设置与它同域下的a.html中的iframe的宽度和高度.
代码如下:
a.html代码
首先a.html中通过iframe引入了b.html
<iframe id=”b_iframe” height=”0″ width=”0″ src=”http://www.alimama.com/b.html” frameborder=”no” border=”0px” marginwidth=”0″ marginheight=”0″ scrolling=”no” allowtransparency=”yes” ></iframe>
b.html代码
<iframe id=”c_iframe”  height=”0″ width=”0″  src=”http://www.taobao.com/c.html” style=”display:none” ></iframe>
<script type=”text/javascript”>
var b_width = Math.max(document.body.scrollWidth,document.body.clientWidth);
var b_height = Math.max(document.body.scrollHeight,document.body.clientHeight);
var c_iframe = document.getElementById(“c_iframe”);
c_iframe.src = c_iframe.src+”#”+b_width+”|”+b_height; //http://www.taobao.com/c.html#width|height”
}
</script>
<!–js读取b.html的宽和高,把读取到的宽和高设置到和a.html在同一个域的中间代理页面车c.html的src的hash里面–>
c.html代码
<script type=”text/javascript”>
var b_iframe = window.parent.parent.document.getElementById(“b_iframe”);
var hash_url = window.location.hash;
var hash_width = hash_url.split(“#”)[1].split(“|”)[0]+”px”;
var hash_height = hash_url.split(“#”)[1].split(“|”)[1]+”px”;
b_iframe.style.width = hash_width;
b_iframe.style.height = hash_height;
</script>
a.html中的iframe就可以自适应为b.html的宽和高了.
其他一些类似js跨域操作问题也可以按这个思路去解决


转自:http://ued.alimama.com/front-end/use-location-hash-to-auto-sizing/
分享到:
评论

相关推荐

    利用location.hash实现跨域iframe自适应

    www.baidu.com,假设地址:http://www.baidu.com/b.html 实现效果: A域名下的页面a.html中通过iframe嵌入B域名下的页面b.html,由于b.html的宽度和高度是不可预知而且会变化的,所以需要a.html中的iframe自适应大小....

    window.location.hash解析

    在JavaScript编程中,`window.location.hash` 是一个非常重要的属性,它用于获取或设置当前URL的哈希值,也就是URL中“#”后面的部分。这个属性在开发网页应用时经常被用来实现页面内的导航和锚点定位。这篇博客文章...

    js跨域问题之跨域iframe自适应大小实现代码

    使用上述方法,可以在不违反同源策略的前提下,通过中间代理页面间接获取跨域页面的尺寸信息,并动态调整iframe的尺寸来实现自适应效果。这种方法既保证了安全性,也解决了跨域带来的限制问题。 具体到代码实现,...

    小发现之浅谈location.search与location.hash的问题

    论location.search与location.hash的问题 在前端开发中,location对象的search和hash属性是两种常用的传参方式,但是它们之间的关系和应用场景却不太为人所知晓。本文将深入探讨location.search与location.hash的...

    js 页面刷新location.reload和location.replace的区别小结.docx

    JavaScript 中的 location.reload 和 location.replace 方法的区别 在 JavaScript 中,我们经常使用 location.reload() 和 location.replace() 两个方法来刷新页面或重定向到新的 URL。但是,这两个方法之间有着...

    IFrame跨域高度自适应实现代码

    本文将深入探讨如何实现IFrame跨域高度自适应的解决方案。 首先,我们需要了解什么是跨域。在Web安全策略中,浏览器执行同源策略,这意味着一个网页只能访问与之同源(协议、域名、端口相同)的IFrame内容。但当...

    什么是跨域解决方案有哪些.docx

    三、 location.hash + iframe 跨域 通过使用 location.hash,可以实现跨域通信。 四、 window.name + iframe 跨域 通过使用 window.name,可以实现跨域通信。 五、 postMessage 跨域 通过使用 postMessage,...

    location.hash保存页面状态的技巧

    hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分)...在我们的项目中,写了一个很简单的JavaScript基类来处理location.hash从而保存页面状态,今天在此就分享给大家。 (本文的内容可能

    跨域解决方案

    3. location.hash + iframe 跨域 4. window.name + iframe 跨域 5. postMessage 跨域 6. 跨域资源共享(CORS) 7. nginx 代理跨域 8. nodejs 中间件代理跨域 9. WebSocket 协议跨域 今天,我们主要讨论 jsonp 跨域...

    jsp页面iframe高度自适应的js代码.docx

    在 jsp 页面中,我们可以使用 js 代码来实现 iframe 的高度自适应。下面是一个简单的示例代码: ```html &lt;iframe id="agentFrame" name="agentFrame" src="" width="0" height="0" style="display:none;"&gt;&lt;/iframe&gt;...

    JS实现iframe自适应高度的方法(兼容IE与FireFox)

    设置`iframe`的高度为这个值,即可实现自适应。 接下来,我们需要在`iframe`标签中添加`id`属性和`onload`事件监听器,以便在`iframe`内容加载完成后自动调用`adjustFrameSize`函数: ```html &lt;iframe id="iframe1...

    前端解决跨域问题的8种方案.docx

    location.hash.substring(1) : ''; if (console.log) { console.log('Now the data is ' + data); } } catch (e) {} } setInterval(checkHash, 2000); ``` **`cs2.html`代码示例:** ```javascript //...

    前端常见跨域解决方案(全).mht

    分享转载:前端常见跨域解决...3、 location.hash + iframe 4、 window.name + iframe跨域 5、 postMessage跨域 6、 跨域资源共享(CORS) 7、 nginx代理跨域 8、 nodejs中间件代理跨域 9、 WebSocket协议跨域

    [removed].hash 属性使用说明

    利用这个属性值可以做一个非常有意义的事情。 很多人都喜欢收藏网页,以便于以后的浏览。不过对于Ajax页面来说的话,一般用一个页面来处理所有的事务,也就是说,如果你浏览到一个Ajax页面里边有意思的内容,想将它...

    jquery.hash.min.js 和 jquery.ba-hashchange.min.js

    而 `jquery.hash.min.js` 和 `jquery.ba-hashchange.min.js` 这两个文件则是在 jQuery 基础上扩展了对浏览器 URL hash 变化的处理,从而实现单页面应用(Single Page Application, SPA)中后退和前进按钮的刷新功能...

    scrollHash.js:滚动到Blaock时,然后更改location.hash!

    `scrollHash.js`的主要功能是监听页面滚动事件,当用户滚动到页面中的某个区块时,它会自动更新`window.location.hash`,使得浏览器URL显示对应的区块标识。这在多段式的长页面设计中特别有用,因为用户可以通过直接...

    js location.replace与location.reload的区别

    需要注意的是,在这个例子中,如果想要实现iframe内页面的刷新,则应该使用parent.document.location.reload()方法,因为window.location.href = parent.location.href将使得iframe加载新的页面,而不是刷新。...

Global site tag (gtag.js) - Google Analytics