简体   繁体   English

将外部 HTML 加载到页面中,包括 Javascript 并在加载时启动 JS

[英]Load External HTML into page INCLUDING Javascript and Initiate JS On load

I need a really lightweight solution for loading an external HTML file into a page with pure JS.我需要一个非常轻量级的解决方案,用于将外部 HTML 文件加载到具有纯 JS 的页面中。 This external page has HTML, JS and CSS. Basically I'm creating a small JS snippit that loads a button + modal (which is in the external JS file) and when the button is clicked the modal appears.这个外部页面有 HTML、JS 和 CSS。基本上我正在创建一个小的 JS 片段,它加载一个按钮 + 模态(在外部 JS 文件中),当单击按钮时模态出现。

I have the button+modal working great, but I can't figure out how to include it on the page so that the JS events trigger.我的按钮+模态效果很好,但我不知道如何将它包含在页面上以便触发 JS 事件。 I can load it with a XMLHttpRequest , but the JS in inactive and the events don't trigger.我可以用XMLHttpRequest加载它,但 JS 处于非活动状态并且事件不会触发。

Below is my sample code (which gets the file but doesn't initiate the JS):下面是我的示例代码(获取文件但不启动 JS):

    <script type="text/javascript">
        var xhr; if (window.XMLHttpRequest){xhr = new XMLHttpRequest(); } else { console.log('Cannot create HTTP Request');}
        xhr.onload = function () {
            if (xhr.status >= 200 && xhr.status < 300) {
                document.getElementById("my--button-container-19763").innerHTML = xhr.response;
            } else {
                console.log('The request failed!');
            }
        };
        xhr.open('GET', 'scripts.html'); xhr.send();
    </script>
    <div id="my--button-container-19763"></div>

Here is the sample code for the button+modal HTML page being loaded:这是正在加载的按钮+模式 HTML 页面的示例代码:


<style type="text/css">
div#my--modal-19763.my--modal{display:none;position:fixed;z-index:1;padding-top:7vh;left:0;top:0;width:100%;height:100%;overflow:auto;background-color:#000;background-color:rgba(0,0,0,0.4)}
div#my--modal-19763.my--modal .my--modal-content{position:relative;background-color:#fefefe;min-height:475px;height:86vh;margin:auto;padding:0;border:1px solid #888;width:80%;box-shadow:0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);-webkit-animation-name:myModal19763AnimateTop;-webkit-animation-duration:.4s;animation-name:myModal19763AnimateTop;animation-duration:.4s;border-radius: 5px;overflow: hidden;border: 2px solid #eee;}
div#my--modal-19763.my--modal .my--close{color: #fff; font-size: 28px; font-weight: 700; z-index: 3; position: absolute; top: 10px; right: 10px; background: rgba(0,0,0,.6); height: 35px; width: 35px; border-radius: 35px; text-align: center; line-height: 35px;}
div#my--modal-19763.my--modal .my--close:hover,
div#my--modal-19763.my--modal .my--close:focus{background: rgba(0,0,0,.8); text-decoration:none;cursor:pointer}
div#my--modal-19763.my--modal .my--modal-header{padding:2px 16px;background-color:#5cb85c;color:#fff}
div#my--modal-19763.my--modal .my--modal-body{padding:2px 16px;position: relative;height: 100%;}
div#my--modal-19763.my--modal .my--modal-footer{padding:2px 16px;background-color:#5cb85c;color:#fff}
div#my--modal-19763.my--modal .my--modal-body-iframe {position: absolute;top:0px; left: 0px; right:0px; bottom: 0px;border: none;}
@-webkit-keyframes myModal19763AnimateTop {
  from{top:-300px;opacity:0}
  to{top:0;opacity:1}
}
@keyframes myModal19763AnimateTop {
  from{top:-300px;opacity:0}
  to{top:0;opacity:1}
}
</style>
<button id="my--modal-19763-btn">Open my--modal</button>
<div id="my--modal-19763" class="my--modal">
  <span id="my--modal-19763-close" class="my--close">&times;</span>
  <div class="my--modal-content">
    <div class="my--modal-body">
      <iframe class="my--modal-body-iframe" src="https://app.my.io" width="100%" height="100%"></iframe>
    </div>
  </div>
</div>
<script type="text/javascript">
  var myModal19763 = document.getElementById("my--modal-19763");
  var myModalBtn19763 = document.getElementById("my--modal-19763-btn");
  var myModalClose19763 = document.getElementById("my--modal-19763-close");
  myModalBtn19763.onclick = function() {
    myModal19763.style.display = "block";
  }
  myModalClose19763.onclick = function() {
    myModal19763.style.display = "none";
  }
  window.onclick = function(event) {
    if (event.target == myModal19763) {
      myModal19763.style.display = "none";
    }
  }

</script>

You won't be able to remote execute injected JS without some messiness .如果没有一些混乱,您将无法远程执行注入的 JS。

The best way to get around this would be to insert an <iframe> into the my--button-container-19763 element on the outer page.解决此问题的最佳方法是将<iframe>插入外页的my--button-container-19763元素中。 Then the content will load there without any need for AJAX. Depending on the styling of the outer page though, this may limit the size of your modal, so it may take some refactoring.然后内容将加载到那里,而不需要 AJAX。不过,根据外部页面的样式,这可能会限制模态框的大小,因此可能需要进行一些重构。

<div id="my--button-container-19763">
    <iframe src="scripts.html">
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM