1

Possible Duplicate:
Permission Denied IE iFrame
Access parent document from dynamic iframe using jquery

I am trying to access the parent document from a dynamic <iframe> child using $('#_hf_iFrame', top.document). It works in Firefox, Chrome and Safari but IE throws an Access Denied exception.

I am using following code to create the <iframe> dynamically.

This question is continuation of this question. I use the following code to append the dynamic to the document.

var _hf_iFrame = document.createElement("iframe");
_hf_iFrame.setAttribute("id", "_hf_iFrame");
_hf_iFrame.setAttribute("name", "_hf_iFrame");
_hf_iFrame.setAttribute("allow-transparency", true);
_hf_iFrame.setAttribute("style", "height: 354px; width: 445px; border: 0; top: 23%; position: fixed; left:0; overflow: show; background:transparent;");
document.body.appendChild(_hf_iFrame);
_hf_iFrame.setAttribute("src", "javascript:false");

var myContent = '<!DOCTYPE html>'
+ '<html><head><title></title><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script><script type="text/javascript" src="http://somedomain.com/js/core.js"></script></head>'
+ '<body style="margin: 0px;"></body></html>';
_hf_iFrame.contentWindow.document.open('text/html', 'replace');
_hf_iFrame.contentWindow.document.write(myContent);
_hf_iFrame.contentWindow.document.close();

How can I solve this?

Community
  • 1
  • 1
Libin TK
  • 1,477
  • 2
  • 25
  • 46
  • Looks like you're trying to ask the same thing as here: http://stackoverflow.com/questions/12947122/access-parent-document-from-dynamic-iframe-using-jquery. Only one question on a given topic. If your other question isn't clear enough (which does seem to be a problem), then edit it. – jfriend00 Oct 18 '12 at 05:41
  • @jfriend00 I am sorry for creating multiple questions on the same topic.. – Libin TK Oct 18 '12 at 05:48
  • Looks like it may be answered here: http://stackoverflow.com/questions/1886547/access-is-denied-javascript-error-when-trying-to-access-the-document-object-of – jfriend00 Oct 18 '12 at 06:05
  • @jfriend00 thank you so much... that link was helpful. But please see my answer – Libin TK Oct 18 '12 at 07:19

3 Answers3

0

This might fix your problem.

Cross Sub Domain Javascript

<script type="text/javascript">
document.domain = "yourdomain.com";
</script> 

Basically, JS believes that even a subdomain such as img.yourdomain.com is a different domain from www.yourdomain.com. Because of that, AJAX across pages from those two subdomains will not work. Also if you have an iframe from one to another, you will not be able to refence JS vars or functions back and forth.

Techie
  • 44,706
  • 42
  • 157
  • 243
0

I have changed the code to

//var source = "javascript:void((function(){document.open();document.domain=\'cloudapp.net\';document.close();})())";  
var source = "javascript:false";
var elem = document.createElement("iframe");
elem.frameBorder = "0";
elem.src = source;
elem.style.width = "100%";
elem.style.margin = "0px";
elem.style.padding = "0px";
elem.setAttribute("id", "_hf_iFrame");
document.body.appendChild(elem);

elem.contentWindow.document.open('text/html', 'replace');
elem.contentWindow.document.write(myContent);
elem.contentWindow.document.close();

and it working on all browsers... but still I couldn't find the reason for the strange issue.

Libin TK
  • 1,477
  • 2
  • 25
  • 46
-1

If both parent and iframe are on the same domain they i think you can get the Parent object from the iframe. Otherwise child iframe or parent cannot access each other.

To set the domain

document.domain = 'yourdomain.com';

Edited:

document.domain can only be set to a subset of the current domain. So developer.mozilla.org can be set to mozilla.org, but not mozilla.com. This property may also be read-only in some browsers.

Talha
  • 18,898
  • 8
  • 49
  • 66
  • 1
    `document.domain` can only be set to a subset of the current domain. So `developer.mozilla.org` can be set to `mozilla.org`, but not `mozilla.com`. This property may also be read-only in some browsers. See [here](https://developer.mozilla.org/en-US/docs/DOM/document.domain) for more info. – jfriend00 Oct 18 '12 at 05:52
  • @jfriend00 I have set the `document.domain` on both document without any error and a `alert(document.domain);` reflects the same. But still IE breaks on `$('#_hf_iFrame', top.document)`. – Libin TK Oct 18 '12 at 05:55
  • @LibinTK see the updated portion... – Talha Oct 18 '12 at 06:58