今天終於更新了網站,卻發現評論這塊居然不能自適應高度,郁悶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
