views:

1795

answers:

3

I have an application that has a certain page -- let's call it Page A. Page A is sometimes a top-level page, but also sometimes is embedded as an iframe within page B. All pages come from the same server and there are no cross-domain issues.

I have a greasemonkey script that runs on page A. How can the greasemonkey script detect whether page A is within the iframe context or not?

A: 

The predicate

(window.parent.frames.length > 0)

will tell you just what you want.

Artem Barger
what a cool site ! +1 for that
76mel
Just don't forget to do it after ;0)
Artem Barger
A: 

Would you just look for at the parent for frames and see if they exist ?

76mel
+8  A: 

Looking at frame length breaks down generally if page A itself has frames (I know this might not be the case for this specific instance). The more reliable and meaningful test would be:

if (window!=window.top) { /* I'm in a frame! */ }
annakata
Can you explain? Is this because a.parent can equal a?
Cheekysoft
yes, exactly so - .top and .parent can point to the same object as .self (and in fact it would possibly be clearer to test window.self != window.top)
annakata