View Full Version : Script to force a page into its parent iframe

03-28-2006, 06:20 AM
I've not been able to find a good script** to use to force a content page to always open in its parent iframe so when Yahoo or Google lists the content page and a user clicks on it, I can make it open up in the iframe that it was made for. Scenario:

Index.html has an iframe with src=contentpage1.htm

contentpage2.htm is another page that should always be opened in the Index.html iframe.

User clicks on a search page external link www.mywebsite.com/contentpage2.htm but page is forced to open inside Index.html iframe.

** it will be 2 scripts, 1 for the to be used in all the content pages and one for the index.html page.

thanks for the help

03-28-2006, 06:47 AM
I'm slightly confused, but isn't this less javascript and more target frame stuff with the links? target=self, target=blank, target=framename? I haven't done this myself and I can't recall specifically from when I learned it. I think its easier than what you're thinking, though.

03-28-2006, 07:19 PM
Target= comes into play only when you are linking from within the iframe or the parent index.html page itself.

My problem is when a user is trying to open up the content page "externally" - usually from an external link like Yahoo or Google. Also if you are in another page in the website and want to link to that content page and want it only to open inside its iframe, you need Javascript to force it. One script in the content page to direct it to open in the parent page and another script in the parent page to then open the content page in the iframe.

03-31-2006, 10:15 PM
I didnt make this script, but i found it and it is rather useful if you dont want your users to view pages outside the frames/iframes. just paste this code into the head section of the pages you dont want users accessing outside the frameset and it will automatically redirect their browsers to the page with the iframe.

<SCRIPT LANGUAGE='javascript'>try { if (top == self) {top.location.href='index.html'; } } catch(er) { } </SCRIPT>

03-31-2006, 10:46 PM
Thanks for the input.

I've tried this type of script and it will open the parent index.html page but it doesn't automatically load the original content page into the iframe. So the user gets redirected to the parent page but now doesn't have the page in the iframe they wanted.

I need a script like this to redirect to the Parent iframe page and a script for the Parent page to load the content page into the iframe.

Does anyone have these two scripts?

03-31-2006, 11:37 PM
thats where links come in... either place the links to other pages inside or outside of the iframe. if the link is placed outside of the iframe use
<a href="contentpage2.html" target="iframename"> (dont forget to name your iframe or this wont quite work) in the iframe tag put in
name="(whatever you want to name your iframe)" of course putting links to other pages inside the iframe though doesnt require the target method.

04-01-2006, 12:02 AM

Please read my reply above. The problem to solve is from external links NOT internal page links.

04-01-2006, 12:30 AM
Why not use target="blank"? That'll open a new, clean window, and it'll leave your site in the background, so they'll return to it after viewing the new page. Might be you some more traffic than otherwise.

04-01-2006, 02:41 AM
OK, Let me restate my javascript need for those who don't want to read the top question:

I need help with a script so when a user clicks on a link, to one of my content (iframe) pages ,that's on on an "EXTERNAL" page to my website and I want that content page to only open in its parent page iframe!

< a href="" target="iframe"> does not come into play here. It only works when the link is on the same page as the iframe.

An example is where I have a link on Google to one of my content (iframe) pages that I only want to have it open in the iframe of the parent index.html.

04-01-2006, 04:27 AM
With a little modification, this should work for iframes:


You can use the same script as suggested for the 'orphan' page as at the above link. Then on the top page use this:

<script type="text/javascript">
var text = window.location.href;
function delineate(str) {
theleft = str.indexOf("=") + 1;
theright = str.indexOf("&");
return(str.substring(theleft, str.length));

function refreshFrame() {

and since it isn't a frameset page, run the refreshFrame() onload:

<body onload="refreshFrame();">

04-01-2006, 05:11 AM
Just for the record, I understood what you were saying the entire time; I was just trying to give you a really easy solution rather than a complex code, but I'm not saying to use it. I just thought it might help.

04-02-2006, 10:24 PM

Your the best, thanks for the code. It works just like I need it to! See at


I did have to use the content page code from your link to the other forum on frames as it needed to pass the window url.


<script type="text/javascript">
if (window == top) {
var url='window.location.replace("http://www.saintjohnlutheran.net/?var1='+window.location.href+'")';

You need the actual name of the iframe parent page, like 'index.html' right before the ?.

Now I don't need to worry about Google and Yahoo links plus I can now link to those content pages from anywhere in my site and they will always open in their intended iframe. - Yes!!!

I'll need to go to those other forums that had code that I could not get to work and link them here. :-)

04-03-2006, 03:46 AM

I have one more piece of help on this that I ran across implementing this across my site.

I have many Parent iframe pages that have "2" iframes. One iframe holds a menu type listing with thumbnails# and a 2nd iframe for the content pages that link to the thumbnail listing. Is there a way to add an "if" statement to the parent iframe page script to perform:

content page url = "thumbnail_listing.htm", load in location "iframe1";
load into location "iframe2"

See http://www.aircraftjournal.com/list/list_fighters.htm

# - too much code for this thumbnail listing to put on the parent page code and the user needs to scroll the list.


02-28-2007, 06:21 AM
if used the code on my new site: http://webwijzer.willemstad.net but for some reason, it keeps re-opening the source of the iframe into a new url.
I start getting the following urls http://webwijzer.willemstad.net/index.asp?var1=http://webwijzer.willemstad.net/index.asp?var1=http://webwijzer.willemstad.net/Cafe+%26+Bar.170 and they get longer and longer adding http://webwijzer.willemstad.net/index.asp?var1= to the front each time, until I hit the STOP button in the browser.
So i'm guessing I'm doing something wrong in the
<SCRIPT LANGUAGE='javascript'>
if (window == top) {
var url='window.location.replace("http://webwijzer.willemstad.net/index.asp?var1='+window.location.href+'")';

Does anyone have any idea why it keeps getting reloading and reloaded again and again.

02-28-2007, 07:55 AM
Does anyone have any idea why it keeps getting reloading and reloaded again and again.

It's because your URL already has a query string included. This method was worked out for ordinary HTML pages, not server-side pages that already use query strings.

The scripts probably can be modified to 'play nice' with pages that already include a query string but, there may be other problems with your particular server side set up.

02-28-2007, 02:58 PM
i've tried it with a plain html page, but that doesn't seem to fix the problem.

02-28-2007, 03:58 PM

a plain html page

Anyways, it isn't the DOCTYPE or the extension of the page that is at issue so much as how your server treats the page and whether or not there is any query, named anchor, and/or other information already being passed in the URL before these scripts add and try to pass and parse their own.

At issue here as well could be how your server may be viewing the added query data from the script.

There could also be other problems like, if you didn't get both scripts right to begin with and/or don't have these scripts, each on the proper page(s).