Frames in Javascript
Frames in Javascript
Let's look at a few examples of how this works. First we need a few
HTML documents to work with. Let's create four different HTML
documents. Here's what the first will contain:
<!DOCTYPE html>
<html>
<body>
<h1>Frame 1</h1>
<p>Contents of Frame 1</p>
</body>
</html>
<!DOCTYPE html>
<html>
<frameset rows="*,*,*,*">
<frame src="frame_1.html">
<frame src="frame_2.html">
<frame src="frame_3.html">
<frame src="frame_4.html">
</frameset>
</html>
Mixing Columns and Rows
Columns and rows of frames can both appear on the same webpage by
nesting one frameset inside of another. To do this, we first create
a frameset and then nest a child frameset within the parent element.
<frameset cols="*,*,*">
<frameset rows="*,*"> <frame src="frame_1.html">
<frame src="frame_2.html">
</frameset>
<frame src="frame_3.html">
<frame src="frame_4.html">
</frameset>
The nested frameset takes the place of the first frame within the parent
element. The nested element can be placed in any position. For example,
if we wanted the nested element to appear in the center position we
would just rearrange the elements like this.
<frameset cols="*,*,*">
<frame src="frame_1.html">
<frameset rows="*,*">
<frame src="frame_2.html">
<frame src="frame_3.html">
</frameset>
<frame src="frame_4.html">
</frameset>
Sizing Frames
Frames can be sized either in pixels or percentages, or they can be set to
automatically adjust in size based on the available space. To specify the
size of a frame, insert the desired value in the cols or rows attribute.
By default, unless the attribute noresize is added to a frame, website
visitors can use their mouse to drag the border between two frames to
resize the frames. If this is undesirable, the attribute noresize can be
applied to a frame element to prevent resizing.
We can create this layout with the following code.
<frameset rows="150px,*">
<frame noresize src="frame_1.html">
<frameset cols="20%,*,20%">
<frame src="frame_2.html">
<frame src="frame_3.html">
<frame src="frame_4.html">
</frameset>
</frameset>
<frameset rows="150px,*">
<frame noresize src="frame_1.html" marginheight="15">
<frameset cols="20%,*,20%">
<frame src="frame_2.html" frameborder="0">
<frame src="frame_3.html" frameborder="0">
<frame src="frame_4.html" frameborder="0">
</frameset>
</frameset>
The marginheight attribute applied to the first frame will add 15px of
margin above and below the content loaded in the first frame.
The frameborder value of 0 removes the borders from around the three
bottom frames.
If we pull up this code in a browser, here's what it looks like.
Note: This property will also work for <frame> elements. However, the
<frame> element is not supported in HTML5.
<html>
<body>
<p>Click the button to change the location of the first iframe element
(index 0).</p>
<br><br>
<iframe src="https://www.w3schools.com"></iframe>
<iframe src="https://www.w3schools.com"></iframe>
<script>
function myFunction() {
window.frames[0].location = "https://www.w3schools.com/jsref/";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through the frames on this page, and change
the location of every frame to "w3schools.com".</p>
<iframe src="https://www.cnn.com"></iframe>
<iframe src="https://www.bbc.com"></iframe>
<iframe src="https://www.nytimes.com"></iframe>
<script>
function myFunction() {
var frames = window.frames;
var i;