[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript IFRAME and FRAME window detect

Posted on 2009-04-15
2
Medium Priority
?
1,004 Views
Last Modified: 2012-05-06
My site all resolves within a master FRAMESET named "master". Everything should always be within this frameset, thus not allowing the URL to change as a user navigates through the site. Now, within this the site has IFRAMES.

What I want to do is put a piece of javascript code within my 404 Error page so that the page will always appear within the "master" frameset. For example, if one of the pages witihin an IFRAME causes the 404 error page to generate, when the page loads, the javascript should detect it is within the IFRAME and then reload itself within the "master" FRAMESET. I do not want it to reload itself as the parent window, thus replacing the URL with the 404 Error path.

I started with the code attached but it doesn't quite work. This gets into some kind of loop that I don't want to be in.
function checkframe(code) {
if(window.top==window){
// Do Nothing
}
else
{
var thisF= this.window
var allF = parent.top.frames;
for(var i=0;i<allF.length;i++)
if(allF[i]==thisF){
var frameName=allF[i].name;
break;
if (frameName!="master") {
parent.location.href = 'error.asp?code='+code;
}
}
}
}

Open in new window

0
Comment
Question by:MDauphinais1
2 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 24157259
Seems a bit messy.

Try this

function checkframe(code) {
  if (top==self) {
   // this page was loaded outside frameset
    top.location.replace('index.html?page='+location.href
  }
  else {
    var thisF= this.window
    var allF = top.frames;
    var frameName = "";
    for (var i=0;i<allF.length;i++) {
      if (allF[i]==thisF) {
        frameName=allF[i].name;
        break;
      }
    }
    if (frameName!="master") {
      parent.location.href = 'error.asp?code='+code;
    }
  }
}

Open in new window

0
 

Author Comment

by:MDauphinais1
ID: 24159467
That works a lot better. Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to count occurrences of each item in an array.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question