Javascript JS继续加载,直到img src更改

Javascript JS继续加载,直到img src更改,javascript,jquery,image,src,Javascript,Jquery,Image,Src,我想监视NAS上的卸载事件。 它有一个单独的页面来卸载所有usb驱动器。 我已经通过iframe将其包括在内,并希望保持站点加载,直到图像源被更改为特定的源(一个绿色条,表示它已完成卸载)。 我之所以想继续加载它,是因为我从另一个站点通过jquery.get函数调用它,我希望它等到卸载过程完成。 如果这只在普通javascript中可能,那就太好了,但如果需要,我还可以包括jquery。 以下是我到目前为止得到的信息: <script type="text/javascript">

我想监视NAS上的卸载事件。 它有一个单独的页面来卸载所有usb驱动器。 我已经通过iframe将其包括在内,并希望保持站点加载,直到图像源被更改为特定的源(一个绿色条,表示它已完成卸载)。 我之所以想继续加载它,是因为我从另一个站点通过jquery.get函数调用它,我希望它等到卸载过程完成。 如果这只在普通javascript中可能,那就太好了,但如果需要,我还可以包括jquery。 以下是我到目前为止得到的信息:

<script type="text/javascript">
function check(){
if (!(document.getElementById("uiView_TestPic").src == "/css/default/images/finished_ok_green.gif")){
window.setTimeout(check(), 2000);
}else{
alert("Done!");
}}
window.onload(check());
</script></head>
<body>
<iframe seamless width="100%" frameborder="0" height="80%" src="http://fritz.box/usb/usb_diskcut.lua?usbdev=all"></iframe>

函数检查(){
if(!(document.getElementById(“uiView\u TestPic”).src=“/css/default/images/finished\u ok\u green.gif”)){
setTimeout(check(),2000);
}否则{
警报(“完成!”);
}}
window.onload(check());
我立即收到一个错误,即元素“uiView\u TestPic”不存在,即使我在运行check之前添加了超时


包含的iFrame站点在加载所有图像后立即停止加载,而不是在整个过程完成后停止加载。

您可以检查元素是否在dom中,然后检查src。像这样的方法应该会奏效:

function check(){
if (document.getElementById("uiView_TestPic") && !(document.getElementById("uiView_TestPic").src == "/css/default/images/finished_ok_green.gif")){
window.setTimeout(check(), 2000);
}else{
alert("Done!");
}}
window.onload(check());

在这里,您将看到src null错误没有发生。但是,如果删除document.getElementById(“uiView_TestPic”)&首先会出现错误,这两个框架必须在同一个域上运行,因为浏览器会阻止一个框架中的代码访问另一个框架中的任何内容(如果它们是不同的域)。如果这两个帧不在同一个域上,那么iframe将必须监视自己,并在完成使用类似的内容时通知另一个帧

如果它们在同一个域上,这就有点复杂了。您必须获得iframe元素(在主文档中)。您必须等待它准备好(因为它必须加载自己的内容)。然后,您必须在iframe中获取文档。然后可以在iframe中找到所需的图像。然后你可以观察它的内容。当然,如果iframe与您的主页是同一个域(由于安全限制),您只能执行这些操作

首先,向iframe添加一个id:

<iframe id="myFrame" seamless width="100%" frameborder="0" height="80%" src="http://fritz.box/usb/usb_diskcut.lua?usbdev=all"></iframe>
    // put this code in your document AFTER the relevant image tag
    // or only run it after the DOM is loaded in your iframe

    // if you can hook into the actual event that knows the unmount
    // is done, that would be even better than polling for the gif to change

    function checkImageSrc(obj) {
        if (obj.src.indexOf("/finished_ok_green.gif") !== -1) {
            // found the green gif
            // notify the parent
            window.parent.postMessage("unmount complete", "*");
        } else {
            // check repeatedly until we find the green image
            setTimeout(function() {
                checkImageSrc(obj);
            }, 2000);
        }
    }

    // find the target in the iframe content
    var target = document.getElementById("uiView_TestPic");
    checkImageSrc(target);
仅供参考,这里有更多关于等待iframe准备就绪的详细信息:


如果您控制iframe中的代码,那么实现这一点的一种更为简洁的方法是在iframe中的代码完成其操作时将消息发布到父窗口。然后父窗口可以只侦听该消息,它将知道操作何时完成

可能看起来像这样。将此代码放入iframe:

<iframe id="myFrame" seamless width="100%" frameborder="0" height="80%" src="http://fritz.box/usb/usb_diskcut.lua?usbdev=all"></iframe>
    // put this code in your document AFTER the relevant image tag
    // or only run it after the DOM is loaded in your iframe

    // if you can hook into the actual event that knows the unmount
    // is done, that would be even better than polling for the gif to change

    function checkImageSrc(obj) {
        if (obj.src.indexOf("/finished_ok_green.gif") !== -1) {
            // found the green gif
            // notify the parent
            window.parent.postMessage("unmount complete", "*");
        } else {
            // check repeatedly until we find the green image
            setTimeout(function() {
                checkImageSrc(obj);
            }, 2000);
        }
    }

    // find the target in the iframe content
    var target = document.getElementById("uiView_TestPic");
    checkImageSrc(target);
然后,在您的主窗口中,您将听到如下所示的已发布消息:

    window.addEventListener("message", function(e) {
        // make sure this is coming from where we expect (fill in the proper origin here)
        if (e.origin !== "http://example.org:8080") return;
        if (e.data === "unmount complete") {
            // unmount is complete - put your code here
        }
    });

#uiView_TestPic
是否在iframe中?如果
uiView_TestPic
位于与JS运行位置不同的iframe中,则必须从iframe获取文档,并将该
文档
getElementById()
一起使用。您当前的代码正在搜索您当前的文档,但pic位于不同的文档中。如何使JS在iframe文档中显示?@user1685565-我添加了一个关于如何在iframe文档中显示的答案。嗯,现在我立即收到“完成”警报,即使站点甚至还没有完成加载。(我更新你的帖子,让你回到0:P)因为
\uiView\u TestPic
不在同一个文档中,
document.getElementById(“uiView\u TestPic”)
永远找不到它(它在错误的文档中查找)。我们必须等待iframe加载,然后从iframe中获取文档,然后从该文档中获取元素,然后查看它是否是正确的图像。该网站正在运行我的树莓pi(本地),另一个是我的路由器。这可能就是为什么你的代码对我不起作用的原因,我在img src更改后添加了一个简单的警报,但什么也没发生。也许还有别的办法?甚至没有iframe?这只是我能想到的第一个也是最简单的方法,打电话给一个网站,等待img src的发布changed@user1685565-嗯,这是一个重要的细节,不在你的问题中。如果浏览器运行在不同的域上,则会阻止从一个帧到另一个帧的任何访问。正如我在回答的最后所说,如果你同时控制两个站点,那么当图像改变时,只需让iframed网站使用
window.postMessage()
到它的父窗口
window.postMessage()
允许在具有不同域的帧之间使用,但仅此而已。@user1685565-我添加了一个示例,使用
window.postMessage()
处理跨源帧。不幸的是,它是路由器操作系统的一部分,我无法编辑它。这意味着没有办法让它工作?@user1685565-当它是一个不同的域时,没有办法从iframe外部访问iframe DOM。我认为你不能从外面监视GIF。