Usually we put our JavaScript <script>
tags at the bottom of an HTML document, right before the closing </body>
tag, with the benefit that they are executed after all the elements are already available in the DOM and some more things.
However, I'm using a frame document1 which does have a <frameset>
instead of a <body>
tag. I don't want to put them in the <head>
of the document because they wouldn't have immediate access the DOM elements below3. And I don't want to use <iframe>
s in a standard body tag either4. I've tried
<head>
<title>Framesets are interesting</title>
</head>
<frameset cols="50%,50%">
<frame id="frame-a" src="a.html">
<frame id="frame-b" src="b.html">
<script type="text/javascript">
console.log("hello world!");
console.log(document.getElementById("frame-a")); // this is what I'm after
</script>
</frameset>
However, the script was not executed at all, it didn't even show up in the DOM inspector. Sure, a <frameset>
may only contain <frame>
and <noframes>
tags. But, is there really no way to get scripts execute after a <frame>
tag?
Just for reference, placing them after </frameset>
like it's sometimes done with <body>
s doesn't work either.
1: Yes, I know they're deprecated. They were just the natural choice2 for my project, a neat side-by-side view that shows two documents and scrolls them together in a sophisticated manner.
2: …and I never used them before, so I wanted to give it a try.
3: That is what I ended up with, after all an onload handler is trivial. Still the question remains, I'm curious.
4: works fine, but requires intricate CSS styling
<frameset>
was only suppose to include<frame>
or<frameset>
tags. – Legend