window.parent in iframe from one subdomain to another

1k Views Asked by At

I have a page which contains an iFrame which in turn contains a form. When this form is submitted, I need certain information to be returned to the parent.

Here's an example of the code I was trying to use to achieve this...

window.parent.$('#imagename').val("<?php echo $imagename; ?>");

The problem is that the page is on sub1.domain.com and the form (in the iframe) is at sub2.domain.com

I have ownership of both these subdomains so is there anything I can do to make this work?

1

There are 1 best solutions below

0
On

You can set the domain in both parent page and iFrame using document.domain='yourdomain.com . This will let you communicate from parent to iframe and vice versa without problems. Browsers will treat them as same domain

See MDN Docs: https://developer.mozilla.org/en-US/docs/Web/API/document.domain