Detect failure to load contents of an iframe

前端 未结 6 1796
迷失自我
迷失自我 2020-12-01 06:59

I can detect when the content of an iframe has loaded using the load event. Unfortunately, for my purposes, there are two problems with this:

  • If there
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 07:40

    I had this problem recently and had to resort to setting up a Javascript Polling action on the Parent Page (that contains the IFRAME tag). This JavaScript function checks the IFRAME's contents for explicit elements that should only exist in a GOOD response. This assumes of course that you don't have to deal with violating the "same origin policy."

    Instead of checking for all possible errors which might be generated from the many different network resources.. I simply checked for the one constant positive Element(s) that I know should be in a good response.

    After a pre-determined time and/or # of failed attempts to detect the expected Element(s), the JavaScript modifies the IFRAME's SRC attribute (to request from my Servlet) a User Friendly Error Page as opposed to displaying the typical HTTP ERROR message. The JavaScript could also just as easily modify the SRC attribute to make an entirely different request.

    function checkForContents(){
    var contents=document.getElementById('myiframe').contentWindow.document
    if(contents){
        alert('found contents of myiframe:' + contents);
        if(contents.documentElement){
            if(contents.documentElement.innerHTML){
                alert("Found contents: " +contents.documentElement.innerHTML);
                if(contents.documentElement.innerHTML.indexOf("FIND_ME") > -1){
                    openMediumWindow("woot.html", "mypopup");
                }
            }
        }
    }
    

    }

提交回复
热议问题