<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>waelchatila.com - ie tag</title>
  <link>http://waelchatila.com:80/tags/ie/</link>
  <description>Notes on Software, Engineering and Science</description>
  <language>en</language>
  <copyright>Wael Chatila</copyright>
  <lastBuildDate>Thu, 11 Mar 2010 03:23:48 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  
  <item>
    <title>Internet Explorer 6 &amp; 7 document.domain bug</title>
    <link>http://waelchatila.com:80/2007/10/31/1193851500000.html</link>
    
      
        <description>
          &lt;b&gt;Disclaimer:&lt;/b&gt; Im not sure this a bug discovery...couldn&#039;t find anything that resembled it after a couple of minutes of googling, or a super silly security restriction.
&lt;p&gt;
I&#039;ve been coding some javascript ajax code that inserts an iframe, and into the created iframe, inserts a form and submits the form to a (cross domain) server. This is an old cross-domain ajax technique by now, but while implementing it I came across this ridiculously annoying bug in IE. The line in red below will result in an &#034;Access Denied&#034; exception being thrown...again, only in IE. Tried this with IE6 under WinXP Media Center Edition SP2, IE7 under WinXP Home Edition SP2, and IE7 Windows Vista Home. Needless to say...it works just fine in FireFox.

&lt;div class=&#034;codeSample&#034;&gt;
&amp;lt;script type=&#034;text/javascript&#034;&amp;gt;
  document.domain = &#039;waelchatila.com&#039;;
  var new_html = &#039;&amp;lt;iframe id=&#034;frm&#034; name=&#034;frm&#034; src=&#034;http://waelchatila.com&#034;&amp;gt;&amp;lt;/iframe&amp;gt;&#039;;
  document.body.innerHTML+=new_html;
  try {
    var iframe_doc = &lt;font color=&#034;red&#034;&gt;document.getElementById(&#034;frm&#034;).contentWindow.document;&lt;/font&gt;
  } catch (e) {
    alert(e);
  }
&amp;lt;/script&amp;gt;
&lt;/div&gt;
&lt;div id=&#034;frm1&#034;&gt;&lt;/div&gt;
&lt;script type=&#034;text/javascript&#034;&gt;
function insertIframe() {
        document.domain = &#039;waelchatila.com&#039;;
            var new_html = &#039;&lt;iframe id=&#034;frm&#034; name=&#034;frm&#034; src=&#034;http://waelchatila.com&#034;&gt;&lt;/iframe&gt;&#039;;
            document.getElementById(&#039;frm1&#039;).innerHTML+=new_html;
var bError = false;
try {
 var iframe_doc = document.getElementById(&#034;frm&#034;).contentWindow.document;
} catch (e) {
   bError = true;
}
alert(bError?&#034;Exception thrown&#034;:&#034;No exceptions thrown&#034;);
}
    &lt;/script&gt;


Try it by clicking the button below. &lt;p&gt;
&lt;button onclick=&#034;insertIframe();return false;&#034;&gt;Try it!&lt;/button&gt;
&lt;p&gt;
This page should be served off of &lt;i&gt;http://waelchatila.com&lt;/i&gt;. As you can see, the document.domain is explicitly set to the domain the page was served from. The iframe src is also from the same server. I&#039;ve tried it with a whole variety of variations, none of the demonstrated here but tested for.
&lt;ul&gt;
&lt;li&gt;iframe&#039;s document.domain = &#039;waelchatila.com&#039;;&lt;/li&gt;
&lt;li&gt;iframe src is empty&lt;/li&gt;
&lt;li&gt;iframe src is &#034;javascript:false&#034;&lt;/li&gt;
&lt;/ul&gt;
There should be no problem accessing the iframe&#039;s content in either case. 
&lt;p&gt;Now, if the &lt;i&gt;document.domain = &#039;waelchatila.com&#039;;&lt;/i&gt; line is removed, it works. In order for this to work, you&#039;ll need to reload this page if you clicked the previous button since document.domain has now already been assigned.

&lt;p&gt;
&lt;div id=&#034;frm2&#034;&gt;&lt;/div&gt;
&lt;script type=&#034;text/javascript&#034;&gt;
function insertIframe2() {
            var new_html = &#039;&lt;iframe id=&#034;frm&#034; name=&#034;frm&#034; src=&#034;http://waelchatila.com&#034;&gt;&lt;/iframe&gt;&#039;;
            document.getElementById(&#039;frm2&#039;).innerHTML+=new_html;
var bError = false;
try {
 var iframe_doc = document.getElementById(&#034;frm&#034;).contentWindow.document;
} catch (e) {
   bError = true;
}
alert(bError?&#034;Exception thrown&#034;:&#034;No exceptions thrown&#034;);
}
    &lt;/script&gt;


&lt;button onclick=&#034;insertIframe2();return false;&#034;&gt;Try it!&lt;/button&gt;
&lt;p&gt;
The same results present itself if the iframe is created with &lt;i&gt;document.createElement(&#039;iframe&#039;)&lt;/i&gt; and &lt;i&gt;document.appendChild(...)&lt;/i&gt;
&lt;p&gt;
I&#039;ve yet to find a workaround for this issue. I&#039;ll be positing another entry if I found out how. Please let me know if you know of one!
        </description>
      
      
    
    
    
    <category>AJAX</category>
    
    <category>Windows</category>
    
    <category>Web</category>
    
    <comments>http://waelchatila.com:80/2007/10/31/1193851500000.html#comments</comments>
    <guid isPermaLink="true">http://waelchatila.com:80/2007/10/31/1193851500000.html</guid>
    <pubDate>Wed, 31 Oct 2007 17:25:00 GMT</pubDate>
  </item>
  
  </channel>
</rss>

