Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在运行js之前,包括html文件并等待所有加载_Javascript_Html_Jquery - Fatal编程技术网

Javascript 在运行js之前,包括html文件并等待所有加载

Javascript 在运行js之前,包括html文件并等待所有加载,javascript,html,jquery,Javascript,Html,Jquery,我需要在我的主index.HTML文件中包含几个HTML文件,并在加载完包含的HTML元素后执行一些代码。我使用以下代码加载HTML文件: function includeHTML() { var z, i, elmnt, file, xhttp; /* Loop through a collection of all HTML elements: */ z = document.getElementsByTagName("*"); for (i = 0; i < z.le

我需要在我的主
index.HTML
文件中包含几个HTML文件,并在加载完包含的HTML元素后执行一些代码。我使用以下代码加载HTML文件:

function includeHTML() {
  var z, i, elmnt, file, xhttp;
  /* Loop through a collection of all HTML elements: */
  z = document.getElementsByTagName("*");
  for (i = 0; i < z.length; i++) {
    elmnt = z[i];
    /*search for elements with a certain atrribute:*/
    file = elmnt.getAttribute("include-html");
    if (file) {
      /* Make an HTTP request using the attribute value as the file name: */
      xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function () {
        if (this.readyState == 4) {
          if (this.status == 200) {
            elmnt.innerHTML = this.responseText;
          }
          if (this.status == 404) {
            elmnt.innerHTML = "Page not found.";
          }
          /* Remove the attribute, and call this function once more: */
          elmnt.removeAttribute("include-html");
          includeHTML();
        }
      };
      xhttp.open("GET", file, true);
      xhttp.send();
      /* Exit the function: */
      console.log("FILENAME: " + file);
      console.log("Finished html loader IF FILE");
      return;
    }
  }
}


现在,如果我正在运行要执行的js代码,那么XHTML请求仍然没有加载完我的所有DOM内容,但是js代码仍然会执行,在继续我的工作之前,我需要确保所有HTML内容都可用。

我认为您的代码在处理JavaScript的
同步性方面基本上存在问题

所以,我不知道您可以使函数
异步
,这样您的代码就不会并行执行。不过,也许这次会议的答案可能会对此有所启发

但是,另一种更简单的方法是使用jquery的
.load()
函数:

function includeHTML() {
  var z, i, elmnt, file;

  /* Loop through a collection of all HTML elements: */
  z = document.getElementsByTagName("*");

  for (i = 0; i < z.length; i++) {
    elmnt = z[i];
    file = elmnt.getAttribute("include-html");

    /* only use elements having this attribute: */
    if (file) {
      elmnt.load(file);
      elmnt.removeAttribute("include-html");
    }
  }
}
函数includeHTML(){
变量z,i,elmnt,文件;
/*循环浏览所有HTML元素的集合:*/
z=document.getElementsByTagName(“*”);
对于(i=0;i

p.S.您使用的代码是的一个实现。因此,该代码作为属性适用于
w3 include html
,我想它也应该适用于
include html
。但是,W3只加载了一个div,而您正在加载很多div,因此删除
返回可能会起作用。

我认为您的代码基本上在处理JavaScript的
同步性方面存在问题

所以,我不知道您可以使函数
异步
,这样您的代码就不会并行执行。不过,也许这次会议的答案可能会对此有所启发

但是,另一种更简单的方法是使用jquery的
.load()
函数:

function includeHTML() {
  var z, i, elmnt, file;

  /* Loop through a collection of all HTML elements: */
  z = document.getElementsByTagName("*");

  for (i = 0; i < z.length; i++) {
    elmnt = z[i];
    file = elmnt.getAttribute("include-html");

    /* only use elements having this attribute: */
    if (file) {
      elmnt.load(file);
      elmnt.removeAttribute("include-html");
    }
  }
}
函数includeHTML(){
变量z,i,elmnt,文件;
/*循环浏览所有HTML元素的集合:*/
z=document.getElementsByTagName(“*”);
对于(i=0;i

p.S.您使用的代码是的一个实现。因此,该代码作为属性适用于
w3 include html
,我想它也应该适用于
include html
。但是,W3只加载一个div,而您正在加载许多div,因此删除
返回值
可能会起作用。

可能会将
脚本
放在调用
includeHTML()的位置
标记中的
可能会有所帮助。将HTML包含放在中时出现的问题是页面尚未加载,无法找到包含语句。如果我将脚本延迟到标题中,脚本仍然会“太晚”执行。这是您正在寻找的吗?另外,@NJ85加载元素的另一种方法是使用jquery的
.load()
函数:。它可能更快,可以解决您的问题。我不确定。试试看。用jquery解决这个问题非常感谢@tanmay garg。另一个优点是我可以摆脱递归行为。也许将
脚本
放在
标记中调用
includeHTML()
的位置会有所帮助。嘿,将HTML包含放在中的问题是页面尚未加载,并且无法找到include语句。如果我将脚本延迟到标题中,脚本仍然会“太晚”执行。这是您正在寻找的吗?另外,@NJ85加载元素的另一种方法是使用jquery的
.load()
函数:。它可能更快,可以解决您的问题。我不确定。试试看。用jquery解决这个问题非常感谢@tanmay garg。另一个优点是我可以摆脱递归行为。@NJ85请告诉我这是否解决了您的问题,如果解决了,请将其标记为已接受。如果没有,请告诉我您面临的问题。@NJ85请告诉我这是否解决了您的问题,如果解决了,请将其标记为已接受。如果没有,告诉我你面临的问题。