views:

140

answers:

2

Hi All,

Let's say I'm a js script living inside a html page. I need to know if I am inside a specific IFrame.

Something like

if (top.location.href === 'http://specificurl/') ...

However I need this working from any domain.

I have also tried adding a value to the window element in the iframe owner and checking on the child with the same result (permission error).

Any suggestions?

Thanks

Guido

+1  A: 

Can't be done unless you have control over both pages/sites/domains (that is, the frame's page AND the main page).

If you do have control, check out the easyXDM framework or google for "cross domain communication iframe"

And the reason this can't be done is simple security. Imagine if you visited a site that contained a frame which appeared to take up the whole page, and then visited your online banking site -> easy to see how malicious javascript could get all sorts of details, which is why it can only be done if the two sites/pages/domains are explicitly coded to work together

Graza
No I dont have control of both domains, I do however have control of the scripts in both pages.
gatapia
Control over the scripts is as good as the domains. Check out the above framework or google search. It takes a bit of getting your head around, but easyXDM seems to do the trick, with cross-browser-compatibility as well.
Graza
A: 

I figured out how I can do it in MY scenario. Fortunately my client script (living in frame) gets loaded dynamically by one of my scripts. I simply added a #anchor to the url and that was accessible inside the frame.

Very straight forward really.

Thanks for the help Graza, that product looks very interesting fortunately I don't need it.

Thanks

Guido

gatapia