Introduction
A lot of developers hate to use frames. Many a site has been ruined by poor use of them. But frames don't have to be evil. I have seen lots of great sites that use frames, but I have also seen lots of sites that don't need frames. If frames are well-designed, they can make a good site better. But I don't have time to get into how to do that. I am going to show you how to fix a problem on many framed sites. Bookmarks.
The Problem
Have you ever found framed page that looked interesting, but didn't have time to read it, so you bookmarked it and when you returned, you were stuck and couldn't find your page? Have you ever wondered if that happens to your site? Depending on how complex your site is, you may be losing visitors because of that.
What happens? You bookmarked the frameset document, and when you return, your browser reads the source and opens the main page in to the main content frame. You want a way to get around that? Well, that comes next.
The Solution
If you look at the
title="Microsoft's Code Center" target="_blank">Microsoft Developer's Code Center you see that they found a solution that is very similar to the one I am going to show you. At the top of the page, there is a link: "Get Page URL". If you click on the link, the page will appear to refresh, but the address bar now says
http://msdn.microsoft.com/code/default.asp?URL=/code/welcome.asp
. If you bookmark that page, you will always go to the frameset with welcome.asp in the main content frame. I am going to show you how to do this in PHP.
Step One: Minor Changes
In your document in the main content frame, put in the following code:
<a href="index.php?content=main.html" title="Synchronize the current page with the address bar">Sync Address Bar</a>
where index.php is the url of your frameset page (if it's not a PHP file, make it one by renaming it). In the
?content=mypage.html
part, main.html is the current html page (the document in the main content frame). The link will look something like this:
Sync Address Bar (This link will not work; click all you want)
Note: Put this on every page you want your visitors to be able to bookmark or find the exact URL.
Step Two: The Framework
In your frameset page (which should now be a PHP file), put the following PHP script at the top of the document:
<?php // If there is no content variable, // assign the value "main.html" to // the variable. if (!content) { $content == "index.html"; }?>
Now for your frame source for the main content frame, add the following PHP into the src attribute for the frame:
<?php echo("$content"); ?>
Your entire framed page would look something like this:
<!-- index.php --><?php // If there is no content variable, // assign the value "main.html" to // the variable. if (!content) { $content == "index.html"; } ?> <html><head><title>My Framed Page</title></head><frameset cols="100,*"> <frame src="nav.html"> <frame src="<?php echo("$content")?>"></frameset></html>
Conclusion
Of course this isn't the only way to offer a solution for bookmarks. You could make a top frame with the Sync Url link, add more frames, and the list goes on and on. This article only went into the very basic way of doing this. I tried to keep it as simple as possible, and you can tweak it on your own to make it work for your site. Also, if you don't use PHP, this could also easily be translated into another server side language. Again, I don't have time to get into that. Have fun with this script, and don't let any more of your users get caught by the URL/bookmark/favorites problem again.