Coding: iframes



Redirecting Child pages
to appear in Parent iframes

Your web page (parent page) pulls in content from multiple sub pages (child pages) via an iframe. This works well if the browser always opens the parent page first. However, should the child pages be found via a search engine or directly typed in the address bar, then only the child page will display.

The common solution is to set up a referer back to the parent page. So if the child doesn't appear within a frame then load the parent page.

The problem with this is the parent frame will always be loaded with it's default child frame. Should you have just one child frame per parent page that would work fine, but usually there is one parent frame that houses many child pages.

In order to always load a child page within it's correct parent page you need to pass the url of the child page back to the parent page so it knows to display that instead of it's default (hard coded) child page.

The following code was developed to seamlessly put a child frame back in it's correct parent page even when directly opened from a search engine or entered in the address bar.


This HTML code should go in the head content area of the child pages:

<script language="javascript" type="text/javascript"> if(self==top) { top.location.href="/?url="+self.location.href; } </script>

and put this script block at the end of your BODY section -- *after* the close of the IFRAME:

<script language="javascript" type="text/javascript"> if (top.location.search.substr(0,5)=="?url=") { self.frames["ifrm"].location.href = top.location.search.substr(5); } </script>

 
 
 
 

1066 Internet - bespoke Hastings web site designs Web Design Hosting SEO Directories