使用本地文件将iframe内容添加到父文档

问题描述 投票:0回答:1

我有一些代码从iframe中的页面中取出内容并将其添加到父页面。 parent和iframe源页面位于同一目录中,因此不会出现跨域问题。

这将在服务器上运行,但显然不是所有文件都在您的计算机上运行本地。在我弄明白之前,我正在撕开我的头发。在本地运行文件只会给cont2一个未定义的值。

我需要在没有服务器的情况下在本地运行,因为我正在处理的文件很敏感。我不知道怎么做。

我尝试在两个文件中设置document.domain =“local.com”,但这不起作用。

有什么办法吗?

谢谢

父页面:

<html><head>
    <script  src="https://code.jquery.com/jquery-3.3.1.js"></script></head><body>
    <iframe id="frame1" src="page1.html"></iframe><br>
    <button id="button2">Something</button>
    <script>$("#button2").click(function(){
        cont2 = $("#frame1").contents().find("div").html();
        console.log(cont2);
        $("#div2").html(cont2);
    });</script>
    <div id="div2">TBD</div>
</body></html>

iFrame来源:

<html>
    <head></head>
    <body>
        <div id="div1">TODO write content</div>
    </body>
</html>
iframe local
1个回答
0
投票

某些版本的IE将允许它开箱即用。似乎它将本地文件视为在同一服务器上。

对于Chrome,您需要暂时禁用一些内容。见:Disable same origin policy in Chrome

© www.soinside.com 2019 - 2024. All rights reserved.