1

I think this would be pretty easy.

I have a page that loads and iframe. The iframe is another site I am working with. I would like to have some javascript executed, on the parent page, if there is a specific page loaded in the iframe.

Does that make sense?

For example: foo.com is my main page with foobar.com loading in an iframe. If user goes to foobar.com/thanks.html I want the script on foo.com to execute.

Appreciate any help.

  • Not possible. You can't access the contents of an iFrame from a different domain. – PitaJ Jan 22 '13 at 16:23
  • Hi there. Thanks for responding. I am not looking to access the contents. Just if url = x in the frame execute the script on parent page. – Dale Thomas Jan 22 '13 at 16:28
  • possible duplicate of [iFrame src change event detection?](http://stackoverflow.com/questions/2429045/iframe-src-change-event-detection) – jbabey Jan 22 '13 at 16:37
  • You can't even detect the URL with only JavaScript. – PitaJ Jan 22 '13 at 16:49

1 Answers1

0

As for detecting when the iframe has redirected this is very simple, you can use the onload attribute on the iframe element to trigger a function, and it will trigger every time the user redirects. However when trying to access the new url once redircted is not possible due to cross site scripting security issues.

you mention however the iframe site is a site you are working with, since that is the case, what you are after may be possible if you have access to the .htaccess file

add

Header add Access-Control-Allow-Origin: http://parentdomain.com

and then on the parent domain

include this html:

<iframe src="http://childdomain" id="childsite" onload="iframeredirected();"/>

and this javascript

function iframeredirected(){
var new_src = document.getElementById("childsite").documentWindow.location.href;
}
Yusaf Khaliq
  • 3,333
  • 11
  • 42
  • 82