iframe元素在onload事件后返回未定义?

时间:2019-11-12 19:05:58

标签: javascript html iframe

编辑:没有人可以阐明这个问题吗?任何事情将不胜感激。 :)

我有一个脚本,应该检查脚本html是否包含给定的字符串。

当这些元素确实存在时,我的代码将引发以下错误:Uncaught TypeError: Cannot read property 'outerHTML' of null

此行:let check = document.querySelector("#iframe_${globalI}").contentWindow.document.querySelector(".Row"+inc).outerHTML

然后我检查该字符串是否包含检查字符串。IE:check.includes("Pre Trip")

如果我直接在控制台中运行此行,它将正常工作并返回true ...那么这是怎么回事。我怎样才能使这张支票通过。?

我在20秒钟的setTimeout之后执行此检查,然后在尝试解决此问题时再次包装在另一个setTimeout中500ms。

此外,我需要注意的是,没有XSS / CORS问题。

这是我的代码。

        function checkRowCount(x){
      console.log("Row count called on "+x);
      let rowCount = 0;
      for(let i = 0; i < 30; i++){
        if(typeof(document.querySelector(`#iframe_${x}`).contentWindow.document.querySelector('.Row'+i)) != 'undefined' && document.querySelector(`#iframe_${x}`).contentWindow.document.querySelector('.Row'+i) != null){
          rowCount++;
        }
      }
      console.log(rowCount);
      return rowCount;
    }

    let globalCompiler = []; //globalCompiler[globalI] = {unit: unitNumber[globalI], data: ["X", " ", "NO POST TRIP]}

    let unitNumber = [1031,1743,1744,1986,3239,3256,3257,4024,4062,4063,4064,4065,4247,4309,4315,4326,4327,4334,4335,4337,4350,4382,4385,7166,7380,7381,8765,8823,8945,8950,8988,10720,17045,17163,40014,40069,40122,40380,80129,80188,80700,80701,80702,80728,80831,80852,80875,"80876","81027","81038","401288","401306","402409","60099T","CH889","CH890","SR31077","T19","U5509","U6660","U6667","U6675","U8854","US1025T"];
    let url = "http://winweb.cleanharbors.com/Vehicle/VehicleTDSearch.aspx?SearchType=DVIR";

    function iframeLoaded(selector, unit, setDate, callback){
      document.querySelector(`#iframe_${selector}`).contentWindow.document.querySelector("#txtStartDate").value = setDate;
      document.querySelector(`#iframe_${selector}`).contentWindow.document.querySelector("#txtEndDate").value = setDate;
      document.querySelector(`#iframe_${selector}`).contentWindow.document.querySelector("#txtVhcleNo").value = unit;
      document.querySelector(`#iframe_${selector}`).contentWindow.document.querySelector("#btnRetrieve").click();
    }

    let loadFinished = {};

    for(let dec = 0; dec < unitNumber.length; dec++){
      loadFinished[unitNumber[dec]] = false;
    }
    console.log(loadFinished);

    for(let globalI = 0; globalI < 3; globalI++){

      globalCompiler[globalI] = {unit: unitNumber[globalI], data: []};

      let iframeObj = document.createElement('iframe');
      iframeObj.id = `iframe_${globalI}`;
      iframeObj.hidden = false;
      iframeObj.src = url;
      iframeObj.onload = () => {
        if (loadFinished[unitNumber[globalI]] == false) {
            loadFinished[unitNumber[globalI]] = true;
            let setDate = "11/01/2019";
            iframeLoaded(globalI, unitNumber[globalI], setDate);
            console.log("iframeloaded called on " + globalI);
            setTimeout(() => {
              setTimeout(() => {
                    let dateCheckObject = {}, rowCount = checkRowCount(globalI), trackingArr = [];
                    if (rowCount == 0) {
                      globalCompiler[globalI].data.push(" ");
                    } else {
                      for (let inc = 1; inc <= rowCount; inc++) {
                        //let check = $('#iframe_'+globalI).contents().find(`.Row` + inc).html().includes("Pre Trip");
                        let check = document.querySelector(`#iframe_${globalI}`).contentWindow.document.querySelector(".Row"+inc).outerHTML
                        if (check.includes("Pre Trip")) {
                          dateCheckObject.pre = true;
                        } else {
                          dateCheckObject.post = true;
                        }
                      }
                      if(dateCheckObject.pre && dateCheckObject.post) {
                        console.log("X");
                        globalCompiler[globalI].data.push("X");
                        dateCheckObject = {};
                      } else if (dateCheckObject.pre == 'undefined') {
                        console.log("NO PRE");
                        globalCompiler[globalI].data.push("NO PRE TRIP");
                        dateCheckObject = {};
                      } else {
                        console.log("NO POST");
                        globalCompiler[globalI].data.push("NO POST TRIP");
                        dateCheckObject = {};
                      }
                }
              },500);
            }, 20000);
        }
    };
      document.body.appendChild(iframeObj);

      console.log("Global Loop called");
    }
    ```

1 个答案:

答案 0 :(得分:0)

一个for循环的计数值太高了...

例如:for (let inc = 1; inc <= rowCount; inc++) 应该是for (let inc = 1; inc < rowCount; inc++)