【问题标题】:Getting local html inside another local html with JavaScript使用 JavaScript 在另一个本地 html 中获取本地 html
【发布时间】:2015-02-27 00:50:53
【问题描述】:

我的 PC 中有一个用于工作的 html 文件,我想将另一个本地 html 文件加载到其中,我希望将它加载到一个 div 中。 <div id="loadHere"></div>

我使用了这个 jquery 函数,但它不起作用$("#loadHere").load("001.html")

当我这样做时,我得到了这个错误:

XMLHttpRequest cannot load file:///C:/Users/**censored**/Desktop/**censored**/html/00.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
jquery.min.js:132 Uncaught TypeError: Cannot read property 'responseText' of null

我知道这样做是出于安全考虑,并且可能没有安全的方法可以将其关闭(例如对于一个文件夹)。

所以我在考虑替代解决方案,如果我的主 html 文件上有一些拖放空间,我可以在其中拖放我想要加载到 <div id="loadHere"></div> 中的本地 html 文件。

这可能吗?如果是,我应该从哪里开始?有什么框架吗?我的想法是,在我将该文件拖放到拖放区域后,它会保存在某个变量中,并且使用该变量可以很容易地将其加载到该 div 中。

谢谢,祝你有美好的一天。

【问题讨论】:

  • 你应该使用一个简单的轻量级网络服务器
  • 我在工作中无法做到这一点,这就是我正在寻找解决方法的原因。
  • 你可以在chrome中关闭--disable-web-security

标签: javascript jquery html


【解决方案1】:

使用 iframe 的最佳和最简单的方法:

<iframe id="loadHere" src="001.html"></iframe>

【讨论】:

    【解决方案2】:

    在 chromium / chrome 浏览器中,尝试将启动器调整为

    /path/to/chromium-browser --allow-access-from-files
    

    镀铬

    /path/to/google-chrome --allow-file-access-from-files
    

    How do I make the Google Chrome flag “--allow-file-access-from-files” permanent?

    List of Chromium Command Line Switches

    【讨论】:

      【解决方案3】:

      适用于任何浏览器的最简单解决方法 - 只需将其加载到 iframe 而不是 div 中:

      &lt;iframe id="loadHere" src="loadHere.html"&gt;&lt;/iframe&gt;

      或者用 JS 设置 src:

      document.getElementById('loadHere').src = "something.html";

      【讨论】:

      • 嗯,这是一个不错的解决方案,但它不会从主 html 文件中获取 CSS。
      • 是的。另一个解决方法是将 css 包含在您加载的任何 html 文件中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      相关资源
      最近更新 更多