In IE, delete of non-existent property fails if object is in another window. Why?

40 Views Asked by At

Normally, code such as:

var x = {};
...
delete x.foo;

should not cause any errors, even if foo is never defined on x.

However, in IE (but not Chrome or Firefox), if delete x.foo; is called from code in a different window than the one x was created in, it throws an "SCRIPT438: Object doesn't support this property or method" error if the foo property doesn't exist. (It works fine if it does exist).

Here's some test code you can save to a HTML file and run yourself:

<!doctype html>
<html>
  <head>
    <title>Window 1</title>
    <script>
      var x = {}, win2, d;
      function makewindow2()
      {
        if (win2&&!win2.closed) win2.focus(); else win2 = window.open("", "win2");
        d = win2.document;
        d.open();
        d.write(
'<!doctype html>\n'+
'<html>\n' +
'  <head>\n' +
'    <title>Window 2</title>\n' +
'    <script>\n' +
'      var win1 = window.opener, win1x = win1.x;\n' +
'    <'+'/script>\n' +
'  </head>\n' +
'  <body>\n' +
'    This is Window 2\n' +
'    <br><br>\n' +
'    <input type="button" onclick="delete win1x.foo;" value="Click to delete property foo from Window 1\'s x object">\n' +
'    <br><br>\n' +
'    <input type="button" onclick="win1x[\'foo\'] = true;" value="Click to set property foo on Window 1\'s x object">\n' +
'  </body>\n' +
'</html>');
        d.close();
      }
    </script>
  </head>
  <body>
    This is window 1
    <br><br>
    <input type="button" onclick="makewindow2()" value="Click to create Window 2">
    <br><br>
    <input type="button" onclick="delete x.foo;" value="Click to delete property foo from the x object">
    <br><br>
    <input type="button" onclick="x['foo'] = true;" value="Click to set property foo on the x object">
  </body>
</html>

(You'll need to click "Allow Blocked Content" for both Window 1 and 2).

You'll note that in Window 1, you can set or delete foo in any order, any number of times. But in Window 2, if you try deleting it before it's set, or deleting it twice in a row, you'll immediately get an error (in IE 11).

My question is,

(a) What's the underlying cause of this behaviour, and

(b) Is this documented somewhere? ('cause I couldn't find it).

(Also, I though a stackoverflow entry documenting this odd behavior might help out people for whom it may be causing difficult-to-diagnose bugs... :-) )

0

There are 0 best solutions below