今天终于更新了网站,却发现评论这块居然不能自适应高度,郁闷ing,用到iframe跨域访问dom,搞定了下面是具体需求和方案!
如果一个网站有多个子域名,任何一个子域名独立网站的页面布局中难免会用到iframe调用,必然会用到iframe自适应高度的js调试,那么存在跨子域问题的调用也会牵扯到访问权限的问题!
例如:页面 http://blog.estorm.cn/web/yincang-html-css.html 里有一个iframe
< iframe id="commlist" height="40" src="http://www.estorm.cn/e/pl/index.php?classid=19&id=4&tempid=2" frameborder="0" width="100%" scrolling="no"></iframe>
当在http://www.estorm.cn/e/pl/index.php?classid=19&id=4&tempid=2这个页面书写js代码让 http://blog.estorm.cn/web/yincang-html-css.html自适应高度的时候就会有访问拒绝的js错误提示!
解决方案:需要在http://blog.estorm.cn/web/yincang-html-css.html页面和http://www.estorm.cn/e/pl/index.php?classid=19&id=4&tempid=2页面都各自加上下面的js代码即可获得访问权限!
<!--
document.domain='estorm.cn';
//-->
</script>
但是上面的解决方案只能在同一主域才能生效,当你尝试替换document.domain的值为其他网站时,会报错,也就是说,上述方法只是对跨子域的解决方案,真正的跨域好像是现在的浏览器还不允许的,呵呵!也可能是我的认识还不到。
至于网上其他方法,我尝试了下并不是很好用,对于我来说这样就可以了。哈哈~~
转自:http://www.estorm.cn/ued/skill/2012-05-21/jQuery-iframe-adaptive-height-and-cross-domain.html