【HTML】iframe跨域訪問問題

概述

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

1.問題重現:

Chrome 版本 41.0.2272.101 (64-bit)html

OS:Win8.1java

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

 

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

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

 

2.Iframe自適應高度代碼

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

<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高度spa

    </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協議不熟悉,請你們不吝賜教。code

相關文章
相關標籤/搜索