title: SCRIPT70: Permission denied when adding options to select element from a recently closed iframe
body: <p>We are having issues when adding options to a select element from an iframe. The code works well in Chrome, Firefox and Opera, but in IE11 we get a "SCRIPT70: Permision denied" when trying to access the recently created option from the parent window.</p>
  
  <p>What we need is to add options to a select element choosing them from a list. The list is shown in a lightbox (with an iframe) and when an element is chosen, it has to be added to the select element and then the lightbox closed (and the iframe destroyed). What we have is something like this (simplified):</p>
  
  <p>A parent window:</p>
  
  <pre><code>&lt;select id="dropdown"&gt;
    &lt;option&gt;1&lt;/option&gt;
    &lt;option&gt;2&lt;/option&gt;
    &lt;option&gt;3&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;!-- in the real code, this iframe is shown when a button is pressed --&gt;
  &lt;iframe src="iframe.html"&gt;&lt;/iframe&gt;
  &lt;script&gt;
    $(document).ready(function() {
      $('#dropdown').on('change', function() {
        console.log($(this).children().last().val());
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>And the iframe.html:</p>
  
  <pre><code>&lt;a href="#" data-val="4"&gt;Add 4&lt;/a&gt;
  &lt;a href="#" data-val="5"&gt;Add 5&lt;/a&gt;
  &lt;a href="#" data-val="6"&gt;Add 6&lt;/a&gt;
  &lt;script&gt;
    $(document).ready(function() {
      $('a').on('click', function() {
        var p = window.parent;
        if(p) {
          var dropdown = p.document.getElementById('dropdown');
          if(dropdown) {
              var opt = new Option($(this).data('val'), $(this).data('val'));
              dropdown.options[dropdown.options.length] = opt;
              //close self
              $('iframe', p.document).remove();
          }
        }
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>If you choose any item of the iframe the option is added to the select element and the iframe is destroyed. And now if you access the recently added option in the onchange event attached to the select element you get a "SCRIPT70: Permision denied". And the weirdest thing is that it doesn't happen the first time the event is triggered. You need to <strong>trigger the onchange event at least twice to get the error</strong>.</p>
  
  <p>You can see <a href="http://jsfiddle.net/lucas29252/2psMW/" rel="noreferrer">a working fiddle</a></p>
  
  <h2>Solution</h2>
  
  <p>If we change the way the option is added to the select element <a href="http://jsfiddle.net/lucas29252/2psMW/2/" rel="noreferrer">it works like a charm</a>.</p>
  
  <pre><code>//dropdown.options[dropdown.options.length] = opt;
  dropdown.appendChild(opt);
  </code></pre>
  
  <h2>Other scenarios that doesn't raise the error</h2>
  
  <ul>
  <li><a href="http://jsfiddle.net/lucas29252/2psMW/3/" rel="noreferrer">If the iframe isn't destroyed, but hidden</a></li>
  <li><a href="http://jsfiddle.net/lucas29252/2psMW/4/" rel="noreferrer">If the option is accessed in an event attached to another object</a> (i.e. onclick event of an anchor)</li>
  </ul>
  
  <h2>The Question</h2>
  
  <p>I wish that someone (maybe some Microsoft employee working on the IE team) could explain the details of this strange behaviour.</p>