www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] JavaScript window.top.frames and IFRAME

  1. #1
    Join Date
    Feb 2012
    Posts
    3

    resolved [RESOLVED] JavaScript window.top.frames and IFRAME

    I was doing some playing around with accessing frames on the DOM via window.top.frames, and noticed that the collection doesn't seem to be populated until after the actual <iframe> tag. My example:

    <html>
    <head>
    <title>Test Page</title>
    <script language="javascript" type="text/javascript">
    var oFrames = window.top.frames;
    </script>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
    alert("before: " + oFrames.length);
    </script>

    <iframe id="myframe" src="testing/jreese/test.asp" name="ok"></iframe>

    <script language="javascript" type="text/javascript">
    alert("after: " + oFrames.length);
    </script>
    </body>
    </html>

    demonstrates how the count changes. Is there a better way to access frames/iframes rather than using the frames collection? If my findings are correct, does this mean you're not able to use the frames[] collection in a .js file that's included at the top of a page to access iframes that appear in the body of the page?

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    You don't attempt to access any DOM element until either you know it has been rendered, by virtue of the fact that your script renders below it, or you use an onload or document.onreadystatechange event handler to initialise your code after the DOM is complete.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Jan 2011
    Posts
    117
    I still remember the time when <script> tag was forbidden to appear on the document <head> completely.
    the script element was allowed on the document head only for browser sniffing code.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles