'How to check if iframe is empty/null/undefined?

This doesn't work:

if(document.getElementById("iframe").innerHTML==''){

Is there a safe browser reliable way to check an iframe if its empty or not?

Thanks



Solution 1:[1]

Check the frame's contentDocument property. IE 7 and earlier support the contentWindow property instead, but there is a simple cross browser example at http://www.w3schools.com/jsref/prop_frame_contentdocument.asp.

A less-reliable method but might be what you want... check the src property.

You can read about other frame properties at http://www.w3schools.com/jsref/dom_obj_frame.asp

Solution 2:[2]

The check of iFrame emptyness can be achieved using Jquery as defined below:

<script type="text/javascript">
    if ($("#iframeId").contents().find("body").is(':empty')) {
        // Apply ur logic
    }
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brad
Solution 2