thorn0
thorn0

Reputation: 10427

What is the best way to find out that access to the parent window has restrictions due to the same origin policy?

My current solution:

function isAccessToWindowRestricted(w) {
  try {
    return !w.location.href;
  } catch (e) {
    return true;
  }
}

Any better ideas? Is there a 'legal' way without try-catch?

Upvotes: 5

Views: 600

Answers (1)

Dan Beam
Dan Beam

Reputation: 3927

there is no good way, as there is no way to test that a value in the parent frame even exists without throwing an Exception.

I just tried a few things, including this:

var parentURL = window.parent && window.parent.location && window.parent.location.href;

and no matter what, it'll throw an Exception due to the same origin policy. however, you can check to simply see if you're in an iframe

function checkInFrame( arg ){ arg = arg || window; return arg.parent == window; }

but to my knowledge you have to use a try { ... } catch( ... ) { ... } block (which is what it's there for).

Upvotes: 3

Related Questions