1

Possible Duplicate:
access parent url from iframe

I've been looking around but can't find a set answer or solution for this. I have 2 websites, www2.domain.net and www3.domain.net both these websites have an iFrame on them that links to www.domain.co.uk/iframe.html

What I want to do is check the parent URL, i.e - is the user accessing www2.domain.net or www3.domain.net so I can show specific information to just people accessing www3.domain.net.

Is this possible?

Thanks, Andrew

Community
  • 1
  • 1
Andrew D
  • 106
  • 1
  • 14

3 Answers3

1

It's possible, but not very straightforward and requires that you have control over all three pages involved.

If you do control all three sites there's cross window messaging, courtesy of window.postMessage(). However, it's a (relatively) new addition to the DOM and unsupported in older browsers like IE 7, IE 6, Safari 4, Firefox 2 and Opera 9.

// Parent:
iframeWin.postMessage("Hello, from "+window.location.host);


// Iframe: 
window.onmessage = function (evt) { alert(evt.origin); }

Another option is to pass the domain via the URL and then parse this at the other end:

<iframe src="www.domain.co.uk/iframe.html?www2.domain.net" />

and the required JavaScript (called from the page inside the iframe):

alert(window.location.search);
//-> "?www2.domain.net"
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • This is not supported in many browsers – Mohsen Sep 27 '11 at 20:11
  • http://caniuse.com/#search=cross-document messaging – Mohsen Sep 27 '11 at 20:12
  • @Mohsen: my brain missed two words out of the answer, tis fixed now. When you say "this", it seems like you're referring to the whole answer. The last part of my answer can be implemented in **any browser**. – Andy E Sep 27 '11 at 20:15
0

It is not. iframes are considered frames and so the cross domain policy applies to them. Since the iframe domain (www.domain.co.uk/iframe.html) is different than the parent domain (www3.domain.net) it cannot access the parent domain. I believe, however, that you can still access the iframe from the parent domain either through the DOM or via window.frames (an array of all frames including iframes).

jtfairbank
  • 2,311
  • 2
  • 21
  • 33
-1

It is not possible in this case (as posted) due to Same Origin Policy. However, it seems that you control all these domains. If so, you could pass the parent domain in the iFrame tag's src attribute using query string and access it in the iFrame page. See @Andy's answer for implementation.

amit_g
  • 30,880
  • 8
  • 61
  • 118
  • It is possible if you are in same domain – Mohsen Sep 27 '11 at 20:10
  • 1
    @Mohsen, the OP has clearly mentioned all the domains. The domains of host page and iframe are different and so the iframe **can't** read parent's location. – amit_g Sep 27 '11 at 20:14
  • @Mohsen, what are you trying to demonstrate with that fiddle? In any case, since the jsFiddle itself renders the result in another iFrame that has a domain of fiddle.jshell.net (different from parent jsfiddle.net), the example you have written produces "Permission denied to access property 'href'" (as expected). – amit_g Sep 27 '11 at 20:21