title: "Access is denied" JavaScript error when trying to access the document object of a programmatically-created <iframe> (IE-only)
body: <p>I have project in which I need to create an &lt;iframe&gt; element using JavaScript and append it to the DOM. After that, I need to insert some content into the &lt;iframe&gt;. It's a widget that will be embedded in third-party websites.</p>
  
  <p>I don't set the "src" attribute of the &lt;iframe&gt; since I don't want to load a page; rather, it is used to isolate/sandbox the content that I insert into it so that I don't run into CSS or JavaScript conflicts with the parent page. I'm using JSONP to load some HTML content from a server and insert it in this &lt;iframe&gt;.</p>
  
  <p>I have this working fine, with one serious exception - if the document.domain property is set in the parent page (which it may be in certain environments in which this widget is deployed), Internet Explorer (probably all versions, but I've confirmed in 6, 7, and 8) gives me an "Access is denied" error when I try to access the document object of this &lt;iframe&gt; I've created. It doesn't happen in any other browsers I've tested in (all major modern ones).</p>
  
  <p>This makes some sense, since I'm aware that Internet Explorer requires you to set the document.domain of all windows/frames that will communicate with each other to the same value. However, I'm not aware of any way to set this value on a document that I can't access.</p>
  
  <p>Is anyone aware of a way to do this - somehow set the document.domain property of this dynamically created &lt;iframe&gt;? Or am I not looking at it from the right angle - is there another way to achieve what I'm going for without running into this problem? I do need to use an &lt;iframe&gt; in any case, as the isolated/sandboxed window is crucial to the functionality of this widget.</p>
  
  <p>Here's my test code:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
    &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
      &lt;title&gt;Document.domain Test&lt;/title&gt;
      &lt;script type="text/javascript"&gt;
        document.domain = 'onespot.com'; // set the page's document.domain
      &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;p&gt;This is a paragraph above the &amp;lt;iframe&amp;gt;.&lt;/p&gt;
      &lt;div id="placeholder"&gt;&lt;/div&gt;
      &lt;p&gt;This is a paragraph below the &amp;lt;iframe&amp;gt;.&lt;/p&gt;
      &lt;script type="text/javascript"&gt;
        var iframe = document.createElement('iframe'), doc; // create &lt;iframe&gt; element
        document.getElementById('placeholder').appendChild(iframe); // append &lt;iframe&gt; element to the placeholder element
        setTimeout(function() { // set a timeout to give browsers a chance to recognize the &lt;iframe&gt;
          doc = iframe.contentWindow || iframe.contentDocument; // get a handle on the &lt;iframe&gt; document
          alert(doc);
          if (doc.document) { // HEREIN LIES THE PROBLEM
            doc = doc.document;
          }
          doc.body.innerHTML = '&lt;h1&gt;Hello!&lt;/h1&gt;'; // add an element
        }, 10);
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I've hosted it at:</p>
  
  <p><a href="http://troy.onespot.com/static/access_denied.html" rel="noreferrer">http://troy.onespot.com/static/access_denied.html</a></p>
  
  <p>As you'll see if you load this page in IE, at the point that I call alert(), I do have a handle on the window object of the &lt;iframe&gt;; I just can't get any deeper, into its document object.</p>
  
  <p>Thanks very much for any help or suggestions! I'll be indebted to whomever can help me find a solution to this.</p>