Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross sub domain iframes and JavaScript

I am working on a CMS site whose domain is:

http://www.acmssite.com 

They have a sub-domain where they store a form system:

http://www.forms.acmssite.com 

I have an iframe on the first that looks at a form in the latter.

I need to run scripts to manipulate the latter from the former and was wondering is this possible?

like image 265
RyanP13 Avatar asked May 18 '11 14:05

RyanP13


People also ask

Is iframe cross domain?

A page inside an iframe is not allowed to access or modify the DOM of its parent and vice-versa unless both have the same origin. So putting it in a different way: document or script loaded from one origin is prevented from getting or setting properties of a document from another origin.

How do I access cross domain iframe?

To access cross-domain iframe, the best approach is to use Javascript's postMessage() method. This method provides a way to securely pass messages across domains.

Is subdomain considered cross domain?

Sub-domains are considered different and will fail the Same Origin Policy unless both sub-domains declare the same document. domain DOM property (and even then, different browsers behave differently).

How do I find if a click event on a cross domain iframe?

You cannot detect click events in cross-domain iframe. However it's not reliable, loose focus does not mean a click, it could be user moving across the website using TAB .


Video Answer


2 Answers

In order for this to not be restricted by the same origin policy, you will probably need to do this in both the pages:

document.domain = "acmssite.com"; 
like image 171
Dark Falcon Avatar answered Sep 28 '22 03:09

Dark Falcon


Yes it is.

var iframe = document.getElementById("your-iframes-id").contentWindow.document; 
like image 27
Cobra_Fast Avatar answered Sep 28 '22 03:09

Cobra_Fast