/    Sign up×
Community /Pin to ProfileBookmark

[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?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliOct 12.2012 — 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 [I]onload[/I] or [I]document.onreadystatechange[/I] event handler to initialise your code after the DOM is complete.
Copy linkTweet thisAlerts:
@Troy_IIIOct 12.2012 — 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.
×

Success!

Help @Erwina spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...