Customized Framesets

This script helps you to create a frameset with a content page other than the homepage. When you write a static frameset, you force users to enter at your homepage. In my own site, I want people to be able to enter anywhere they want but nonetheless be in my frameset. This script arranges it.

In fact, this whole script is very simple. First of all you need to find out what page should be shown in the content frame, then you write out the frameset with JavaScript and fill in the correct page in the SRC of the content frame.

The script

In the index page, where the frameset is defined, I use the following script:

var query = (location.href.indexOf("?")+1);
var page = 'intro.html'
if (query)
{
	page = location.href.substring(query);
}

document.write('[rest of frameset]');
document.write('<frame name="content" src=' + page + ' scrolling="auto">');
document.write('[rest of frameset]');

Explanation

This script uses some string methods. See the strings page for more info on indexOf and substring.

So now the user loads index.html?framecustom.html . In itself, this solves nothing: without some extra scripting the user would still see the homepage because by itself the browser ignores the ?framecustom.html bit.

That's where the script on the index page comes into play. First of all, it checks if there's a ? in the URL and puts the index number + 1 of the question mark in query. I add 1 because that makes the script more easy.

var query = (location.href.indexOf("?")+1);

Then I define the variable page and put the default value in it: the homepage:

var page = 'home.html'

Finally, if there's a ? in the URL (if query is not zero), read out the bit that's behind the ? and put it in page:

if (query)
{
	page = location.href.substring(query);
}

Now page contains the pagename behind the ? and if there's no ? (if the user simply goes to index.html) it contains the homepage. So all we need to do is write out the frameset and use page as the source of the content frame:

document.write('[rest of frameset]');
document.write('<frame name="content" src=' + page + ' scrolling="auto">');
document.write('[rest of frameset]');

The only disadvantage of this method is that browser without JavaScript can't enter your site, but since all frame supporting browsers also support JavaScript I don't mind too much. Besides, my site is about JavaScript so I expect people to come here with a JavaScript browser.

Noframes homepage

You can also use this script to lead users from a noframes homepage into a framed site. It works exactly the same:

var query = (location.href.indexOf("?")+1);
var page = 'YourDefault.html'
if (query)
{
	page = location.href.substring(query);
}

document.write('[rest of frameset]');
document.write('<frame name="content" src=' + page + ' scrolling="auto">');
document.write('[rest of frameset]');

On the noframes homepage, simply call the frameset with the correct pagename behind the ?:

<a href="frameset.html?firstpage.html">The first page</a>
<a href="frameset.html?directory/secondpage.html"
	>The second page in another directory</a>