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

Add iframe content to parent documet using local files

我有一些代码可以从 iframe 中的页面中取出内容并将其添加到父页面。父页面和 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>

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

对于 Chrome,您需要暂时禁用几项功能。看: Disable same origin policy in Chrome