views:

2968

answers:

4

Hi, I need to access the Parent Domain URL from my Iframe which is in another domain.
For example: example.com is my website which is in Iframe of another parent(google.com) domain. Here i need to access the parent domain URL in my example.com. i.e I need to get the URL google.com in my example.com. Parent domain cannot be hard coded.

I tried with the following code window.parent.location.href()

this results in Access denied error. How to achieve this..?

A: 

What you are trying to do is impossible for a very good security reason.

Your question is like asking "How can I login as root into this server if I do not know the password?" The only way is: a). Breaching security. b). Being given additional priveledges by the user. (browser specific)

EFraim
Is there any other way to get it done..?
+2  A: 

Hi,

You might want to take a look at these questions/answers ; they could give you some informations concerning your problem :

To make things short : accessing iframe from another domain is not possible, for security reasons -- which explains the error message you are getting.


The Same origin policy page on wikipedia brings some informations about that security measure :

In a nutshell, the policy permits scripts running on pages originating from the same site to access each other's methods and properties with no specific restrictions — but prevents access to most methods and properties across pages on different sites.

A strict separation between content provided by unrelated sites must be maintained on client side to prevent the loss of data confidentiality or integrity.

Pascal MARTIN
A: 

This is not possible.

top.location.href

Will work only if they are in the same domain.

Similar question : Is there a way to get the parent URL from an Iframe’s content?

Shoban
+1  A: 

You can try and check for the referer, which should be the parent site if you're an iframe

Yuliy