PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 15, 2022

[FIXED] How to check if iframe is empty/null/undefined?

 April 15, 2022     browser, html, iframe, javascript, jquery     No comments   

Issue

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

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.



Answered By - Alex
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,216,025

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing