0

Sorry if this question seems basic, but I cannot find any simple explanations and I'm new to JavaScript, so please give any help/feedback you can.

Basically I have an HTML iframe called iframe, and I would like a JavaScript if-statement to see if any <table> objects are in it. I don't know exactly how to reference that in the if-statement. Any help would be much appreciated.

Ian Moore
  • 35
  • 1
  • 8
  • Possible duplicate of [jQuery, select element inside iframe, which is inside an iframe](https://stackoverflow.com/questions/15343955/jquery-select-element-inside-iframe-which-is-inside-an-iframe) – Ullas Hunka Jul 11 '18 at 09:08
  • If the URL of – Jason Jul 11 '18 at 09:37

2 Answers2

0

As far as I know you usually cannot access the content of an iframe due to Same Origin policy.

Anorionil
  • 505
  • 2
  • 7
  • 17
0

So you want to check whether there is any table in the frame.

For that, give the iframe an id , like

<iframe id ="myFrame">
    <!-- your code goes here -->
</iframe>

then in the JavaScript code:

var frame = document.getElementById("myFrame");
var len = frame.getElementsByTagName("TABLE").length;
if ( len == 0 ){
  // the frame contains no tables
}
else{
   // the frame contains one or more tables
}

Hope this helps

Abhinav
  • 42
  • 8