Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q151 How can I set the contents of one of the frames of a frameset when loading the frameset?

You are here: irt.org | FAQ | JavaScript | Table | Q151 [ previous next ]

Normally a frameset is loaded with the use of a simple link:

<a href="frame.html">load frame</a>

Where the content of frame.html is typically:

<html>
<frameset rows="50%,*">
    <frame src="apage.html" name="upperFrame">
    <frame src="bpage.html" name="lowerFrame">
</frameset>
</html>

If you want to choose which document is loaded in the lowerFrame you need to do one of two things. First change the link so that you pass some indication to the frameset of the file to be loaded:

<a href="frame.html?mypage.html">mypage</a>

The frameset needs to be adjusted so that it accepts the parameter passed:

<html>
<script language="JavaScript"><!--
document.write('<frameset cols="50%,*">');
document.write('<frame src="apage.html" name="upperFrame">');
document.write('<frame src="' + (location.search ? location.search.substring(1):'bpage.html') + '" name="lowerFrame">');
document.write('</frameset>');
//--></script>
</html>

If no parameter is passed then it defaults to the bpage.html in the lowerFrame.

If you want to load two frames at once, then you would need to have two parameters on the url:

<a href="myframeset.html?page1.html;page2.html">Click to load two frames</a>

and then split the parameters in the frameset page:

<html>
<head>
<script language="JavaScript"><!--
passed = "" + window.location.search;
if (passed != "" && passed != "undefined") passed = passed.substring(1);

function loadFrames() {
   top.frame1.location = top.passed.substring(0,top.passed.indexOf(';'));
   top.frame2.location = top.passed.substring(top.passed.indexOf(';')+1);
}
//--></script>
</head>

<frameset onLoad="top.loadFrames()">
<frame name="frame1" src="javascript:' '">
<frame name="frame2" src="javascript:' '">
</frameset>

©2018 Martin Webb