nschomer
nschomer

Reputation: 35

detecting location.href in a frame

I am running into a problem on some computers accessing my website when a function call attempts to detect the location.href for a frame, while there is no problem in changing the value. The frame in question might point to different sites, which can be manipulated by clicking on an option in another frame via a call like the following:

parent.frames[0].location.href="http://www.foo.com/bar.html";

which works fine in all cases and changes the frame to the appropriate location. Subsequent attempts to verify if it is in a specific location fail for some users however. I attempt to verify the current frame location via:

if(parent.frames[0].location.href == "http://www.foo.com/bar.html")
{
    return true;
}

for some of my users however, this causes an error which crashes the frame, and is shown in firefox browser (under web developer/error console) as "unable to detect location.href". Yet it works fine for other users using the exact same browser.

My question:

  1. Is there a setting which is causing this error that the client could set to allow checking of location.href values?
  2. Is there a more robust way to check a location which would work despite settings?

The only thing I saw in a web search which looked related was a program called "firebug", but my user's client is not using this program.

Upvotes: 0

Views: 6554

Answers (2)

Gabriele
Gabriele

Reputation: 11

I got a similar problem with the follow code, to breakout a frame:

function breakout_of_frame()
{

    if (parent.frames.length > 0) {
        parent.location.href = self.document.location
    }
}

I didn't find the source of problem, but i found a workaround to make the thing works: I added a little delay.

    function sleep(milliseconds) {
      var start = new Date().getTime();
      for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
          break;
        }
      }
    }


function breakout_of_frame()
{
    sleep(500);
    if (parent.frames.length > 0) {
        parent.location.href = self.document.location
    }
}

I hope this can help you!

Upvotes: 1

aknatn
aknatn

Reputation: 673

Firebug is very helpful for debugging. Whenever I do AJAX or JavaScript I always keep firebug open. RE: detecting page location in a frame, I would personally use a backend language language to read the HTML headers to check for the page details in the frame. In PHP you could use $_SERVER['PHP_SELF'] among others. To debug the JavaScript I would want to re-create the issue on my machine or in a virtual machine.

Upvotes: 0

Related Questions