0

I am using frames on index.html file. On every page of the frame I have a code which checks if page is in frame, and if not, then redirect to index.html.

Now. I want to not only check if page is in frame and redirect to index.html, but also I want to open the page in one of the frames on index.html.

I have embedded the JS file with this code as of right now:

if (top.location == self.location)
{
    top.location = 'index.html'
} 

Is there any scripts you might know of?

HelpNeeder
  • 6,383
  • 24
  • 91
  • 155

1 Answers1

2

You need to make the code in the subpages append their name to the 'index.html', e.g.

if (top.location == self.location) {
    top.location = 'index.html?' + location.href;
}

...and put another Javascript on the index.html page which checks for the part after the question mark, e.g.:

<script type="text/javascript">
window.onload = function() {
  //check if a query string is given
  if (location.search && location.search.length > 1
       && location.search.charAt(0) == "?") {

      //load the URL to the frame
      window.frames["myframe"].location.href = location.search.substring(1);
  }
}
</script>

By the way, you need to give your target frame a name like this:

<frameset ...
    <frame name="myframe" ...
Mira Weller
  • 2,406
  • 22
  • 27
  • I can't make it work :/ It moves me but changes the address in address bar to: `http://192.168.0.2/csharp%20projects/index.html?http://192.168.0.2/csharp%20projects/sitemap.html` from: `http://192.168.0.2/csharp%20projects/` – HelpNeeder Mar 01 '12 at 11:22
  • It changes the address to tell the frameset page what subpage it should load. I think you can't avoid that the address changes. This is a limitation why many people don't like framesets. – Mira Weller Mar 03 '12 at 18:23
  • 1
    Interesting answers: [Why should I not use HTML frames?](http://stackoverflow.com/questions/1203068/why-should-i-not-use-html-frames) ;-) – Mira Weller Mar 03 '12 at 18:23
  • Yes, I would be happy to use something more than just HTML and JavaScript. But I am working on class project and I am limited wits possibilities. But the positive side of the frames is that it is supported across many browsers and there's no real problems by using it. Everything worker perfect until I had to overcome the redirecting problem. This [nswer](http://stackoverflow.com/a/9530876/720323) brought me on a right track :) – HelpNeeder Mar 04 '12 at 02:38