【HTML】iframe跨域訪問問題


概述

本地同一瀏覽器訪問本地HTML文件和訪問服務器端HTML文件,本地Iframe沒有自適應高度,而服務器端的Ifrane自適應了高度。

1.問題重現:

Chrome 版本 41.0.2272.101 (64-bit)

OS:Win8.1

Chrome訪問服務器端HTML文件呈現的結果

 

Chrome訪問本地HTML文件呈現的結果

本地訪問的HTML文件Iframe沒有根據Iframe里面的頁面類容自適應高度

 

2.Iframe自適應高度代碼

在index.html文件中間中添加Iframe頁面,頁面加載時,加載src指定的文件路徑

<iframe id="indexFrame" name="index" width="800" onload='iFrameHeight("indexFrame")' src="Web/Index/indexIframe.html"
           frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

 

JS腳本自適應調整Iframe高度

    </script>
    <script type="text/javascript" language="javascript">
        function iFrameHeight(id) {
            var ifm = document.getElementById(id);
            var subWeb = document.frames ? document.frames[id].document : ifm.contentDocument;
            if (ifm != null && subWeb != null) {
                ifm.height = subWeb.body.scrollHeight;
            }
        }
    </script>

 

3.調試代碼

Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

 

在這里,我猜測是訪問本地文件是file協議(file:///),HTML代碼和JS代碼存在跨域問題。小弟對file協議不熟悉,請大家不吝賜教。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM