title: Java Script, Difficulty getting list of all nested frames in page
body: <p>Hello I need to get list of all frames for my <strong>GreaseMonkey</strong> script but in fact i guess it is <strong>general Javascript question</strong>. It would be great if I could get to every single frame that is nested in page. So far i got problem getting number of frames nested in main document frames.</p>
  <p>Page I'm dealing with consist of <strong>frameset</strong> that is <strong>multilevel</strong> and frames contains other frames. I've managed to get information about top-level frames of main document frameset (in code <strong>level 1</strong>) but at this level i get information that frame count for these frames equals 0 which is not true.</p>
  <p>I've came up with following code</p>
  <pre><code>$(document).ready(function(){
  var frames = window.frames;
  var i,j;
  var reportText = &quot;level 0 &gt; &quot; + frames.length +&quot;\r\n&quot;;
  
  for (i = 0; i &lt; frames.length; i++) {
      var frames2 = frames[i].frames;
      reportText += &quot;level 1 - &quot; + i + &quot; &gt; &quot; + frames[i].name + &quot; - &quot; + frames2.length +&quot;\r\n&quot;;
  
      for (j = 0; j &lt; frames2.length; j++) {
        var frames3 = frames2[j].frames; 
        reportText += &quot;level 2 - &quot; + i + &quot; - &quot; + j + &quot; &gt; &quot;  + frames2[j].name + &quot; - &quot; + frames3.length +&quot;\r\n&quot;;
      }
  }
  
  alert(reportText);});
  </code></pre>
  <p>So <strong>level 0</strong> which is in fact main document frame count and <strong>level 1</strong> names or main document frames - these things are reported correctly but not the amount of frames of each <strong>level 1</strong> frame. And i want to know if it is because my code got mistakes or maybe because subframes are not yest fully loaded.</p>
  <p>I've tried to call my code from keyboard shortcut, after all will look like its completely loaded, but here another problem, seems like following code seems to not to work with page that consist only of frameset</p>
  <pre><code> (function(){
   document.addEventListener('keydown', function(e) {
   if (e.keyCode == 72 &amp;&amp; !e.shiftKey &amp;&amp; !e.ctrlKey &amp;&amp; e.altKey &amp;&amp; !e.metaKey) {
   
   //...my previous code inside document.ready...
   
     }
   }, false);})();
  </code></pre>
  <p>Best would be if code automatically comb through all frames and subframes but with its current shape (where each level got it own loop)  is also good.</p>
  <p>Problem with inability of using shortcut key is secondary. <strong>Main thing is getting proper counts of frames inside frames of main document and beyond.</strong></p>
  <p><em><strong>EDIT:</strong></em> Sample output and My test page with nested frameset</p>
  <p><strong>Output</strong></p>
  <blockquote>
  <p>level 0 &gt; 3</p>
  <p>level 1 - 0 &gt; main1 - 0</p>
  <p>level 1 - 1 &gt; main2 - 0</p>
  <p>level 1 - 2 &gt; main3 - 0</p>
  </blockquote>
  <p><strong>Test page with nested frameset</strong></p>
  <p><strong>frame0.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;frameset cols=&quot;25%,*,25%&quot;&gt;
    &lt;frame id=&quot;frmain1&quot; name=&quot;main1&quot; src=&quot;frame0_1.htm&quot;&gt;
    &lt;frame id=&quot;frmain2&quot; name=&quot;main2&quot; src=&quot;frame0_2.htm&quot;&gt;
    &lt;frame id=&quot;frmain3&quot; name=&quot;main3&quot; src=&quot;frame0_3.htm&quot;&gt;
  &lt;/frameset&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_1.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;frameset rows=&quot;25%,*&quot;&gt;
    &lt;frame id=&quot;frsub11&quot; name=&quot;sub11&quot; src=&quot;frame0_1_1.htm&quot;&gt;
    &lt;frame id=&quot;frsub12&quot; name=&quot;sub12&quot; src=&quot;frame0_1_2.htm&quot;&gt;
  &lt;/frameset&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_1_1.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body style=&quot;background: darkorange;&quot;&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_1_2.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body style=&quot;background: lightyellow;&quot;&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_2.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;frameset rows=&quot;25%,*,25%&quot;&gt;
    &lt;frame id=&quot;frsub21&quot; name=&quot;sub21&quot; src=&quot;frame0_2_1.htm&quot;&gt;
    &lt;frame id=&quot;frsub22&quot; name=&quot;sub22&quot; src=&quot;frame0_2_2.htm&quot;&gt;
    &lt;frame id=&quot;frsub23&quot; name=&quot;sub23&quot; src=&quot;frame0_2_3.htm&quot;&gt;
  &lt;/frameset&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_2_1.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body style=&quot;background: skyblue;&quot;&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_2_2.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body style=&quot;background: cornflowerblue;&quot;&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_2_3.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body style=&quot;background: slateblue;&quot;&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_3.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;frameset rows=&quot;25%,*&quot;&gt;
    &lt;frame id=&quot;frsub31&quot; name=&quot;sub31&quot; src=&quot;frame0_3_1.htm&quot;&gt;
    &lt;frame id=&quot;frsub32&quot; name=&quot;sub32&quot; src=&quot;frame0_3_2.htm&quot;&gt;
  &lt;/frameset&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_3_1.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body style=&quot;background: darkgreen;&quot;&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>frame0_3_2.htm</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body style=&quot;background: lightgreen;&quot;&gt;
      &lt;a id=&quot;test&quot; href=&quot;http://www.google.com&quot;&gt;testlink&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>