javascript - 如何从外部url加载html源模板

标签 javascript html vue.js webpack

我必须使用包含 Logo 、页眉和页脚等的完整源 html 模板作为我的网页的基础。

<html>
<head>
Template content
</head>
<body>
  <div>Template content</div>
  <div>Template Content</div>
  <div #id="customapp">Webpage Content</div>
  <div>Template Content</div>
</body>
</html>

我要替换页面中的一个元素,例如 <div #id="customapp"></div>使用我的自定义应用程序。

有没有办法创建一个 html/javascript 页面,动态加载和呈现模板 html,并在给定模板 html 的托管 URL 的情况下包含我的应用程序?

例如我想要实现的目标:

使用上述模板和模板 url 转换以下 html 应用程序,

<div>My Custom App Content</div>

进入

<html>
<head>
Template content
</head>
<body>
  <div>Template content</div>
  <div>Template Content</div>
  <div>My Custom App Content</div>
  <div>Template Content</div>
</body>
</html>

我的目标是让源 html 的任何进一步更改自动反射(reflect)在网页上,而无需我执行任何操作。

该应用程序当前正在使用 vue 和 webpack。

最佳答案

您可以通过在 iframe 内调用外部模板来加载它。以下 loadExternalWebpage 函数创建一个 iframe,在 iframe 中加载模板并将其附加到 customapp div。

但是要使用 iframe,您应该在加载外部 URL 时意识到安全风险

<html>
  <head>
   Template content
  </head>
  <body>
    <div>Template content</div>
    <div>Template Content</div>
    <div #id="customapp">Webpage Content</div>
    <div>Template Content</div>
 </body>
 <script>
    function loadExternalWebpage() {
      const selector = document.getElementById('customapp');
      const iframe = document.createElement('iframe');
      iframe.src = 'your_external_url';
      selector.appendChild(iframe);
   }
   loadExternalWebpage(); 
 </script>
</html>

关于javascript - 如何从外部url加载html源模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56829086/

相关文章:

html - 歌剧布局问题

html - 如何在 <pre> 标签内转义 < 和 >

node.js - 如何更改 _nuxt 文件夹的名称?

javascript - 使用 var id 获取图像位置

javascript - 如何使用js更改自动响应中的行

javascript onsubmit 在同一调用中返回两个不同的值

vue.js - 如何将事件绑定(bind)到 Vuetify 中的 TreeView 节点?

vue.js - Bootstrap-Vue 表单文件输入在删除时不检查文件格式

javascript - 使用jQuery在下拉列表中设置默认值

javascript - 如何在浮点图中间隔刻度?