最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if iframe is emptynullundefined? - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Nov 18, 2010 at 14:43 user188962user188962 2
  • What do you mean by "empty"? Are you trying to figure out if no page was loaded or if the loaded page is blank? – Richard Marskell - Drackir Commented Nov 18, 2010 at 14:45
  • No page loaded... But both would be good to know... – user188962 Commented Nov 18, 2010 at 14:45
Add a comment  | 

3 Answers 3

Reset to default 8

Well if you can use jQuery, check it's length property. This is cross-browser compatible. If it's zero, it doesn't exist. Something like this:

if(!$("#iframeid").length) {
    // iframe doesn't exist
}

EDIT:

After seeing your comments on your question:

If you want to check if no page loaded inside iframe, and the iframe is not cross-domain you could check for the existence of the body tag inside of the iframe. If it exists, then something loaded.

Something like this:

if($("#iframeid").contents().find("body").length) {
    // some html page loaded in iframe
}

If the iframe is cross-domain, you will be blocked by the same-origin policy. Otherwise this will work.

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

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>
发布评论

评论列表(0)

  1. 暂无评论