将“DOMContentLoaded”附加到 iframe?

Ale*_*lex 6 javascript iframe dom

所以我创建了一个 iframe,它应该在函数运行后继续重新加载。该函数读取 iframe 中的信息。目前我有这样的东西(有效);

function loaded(){
    alert(iframe.contentDocument.getElementsByClassName("blah")[0].innerHTML);
    iframe.src = filePath; //reloads the iframe
}

iframe.onload = loaded;
Run Code Online (Sandbox Code Playgroud)

因为我希望它执行得更快,所以像这样的东西会起作用吗?一旦 iframe 加载 DOM,该函数就会运行;

function loaded(){
    alert(iframe.contentDocument.getElementsByClassName("blah")[0].innerHTML);
    iframe.src = filePath; //reloads the iframe
}

iframe.addEventListener("DOMContentLoaded", loaded, false);
Run Code Online (Sandbox Code Playgroud)

小智 0

iframe.onreadystatechange = () => {
    if (iframe.readyState === "interactive") {
        // your code here
    }
};
Run Code Online (Sandbox Code Playgroud)

我明白,这是一个非常古老的问题,但是当我寻找解决这个问题的方法时,我使用谷歌找到了这个问题