0

So I have a window property variable that I need to delete, and of course IE hates the delete method. I've tried the solution found here, like so:

try {
   delete window.locator_init_in_progress;
}
catch (e) {
   window.locator_init_in_progress = undefined;
}

Now I get the following error in IE8:

enter image description here

What do I need to do to make this stop throwing errors and work in IE8? (I'm assuming it's the try part that's throwing the error, but this code will not pass our QA process if there are any errors in the console, so what do I need to do so this works? Should I just abandon the try/catch and do a test for user-agent, and if it's IE8, set the property to undefined?

UPDATE

Yes, the variable is defined. I've added some alerts in, since IE8 can blow up on console.logs. Here's my current code:

// window.ie8orearlier is a global variable that is set 
// based on user agent string; in this case it's correctly 
// evaluating to true when testing in IE8

if(window.ie8orearlier && window.locator_init_in_progress!= undefined && window.locator_init_in_progress == true) {
   alert("ie8 and value is set; not undefined");
   window.locator_init_in_progress = undefined;
}
else if(window.ie8orearlier && window.locator_init_in_progress == undefined) {
   alert("ie8 and undefined; do nothing");
}
else {
   alert("else");
   delete window.locator_init_in_progress;
}

It's consistently hitting the "ie8 and value is set; not undefined" alert. The error, as far as I can see (IE8's dev tools not providing line numbers for errors), is being thrown by the line trying to set the value to undefined.

Community
  • 1
  • 1
EmmyS
  • 11,892
  • 48
  • 101
  • 156
  • Post more info. Are you sure that's the point where it's failing? – vol7ron Aug 20 '13 at 18:59
  • has is been _defined_ in the first place ? – c69 Aug 20 '13 at 19:05
  • Yes, it's been defined. I'm updating the OP with more info. – EmmyS Aug 20 '13 at 19:14
  • `(function(window.locator_init_in_progress)){ window.locator_init_in_progress = undefined; }();` Wrap this into `IIFE` and tell it to allow overriding, that should work. – Yang Aug 20 '13 at 20:23
  • What is IIFE? And what am I allowing to be overridden? – EmmyS Aug 20 '13 at 20:24
  • As you know if you try to do something like this : `document = null` or `window = null` it will throw an exception too. `Immediately-Invoked-Function-Expression` (IIFE) allows to override non-override able object and properties within its body. `(function(window, document){ window = null; document = null; /* Now both window and document are null, but within its body only*/ })();` – Yang Aug 20 '13 at 20:29

1 Answers1

1

Apparently this is a documented issue in IE8 - see Kangax's thorough write-up on the delete keyword here. Trying to use delete to remove any object property, not just window properties, in IE 8 throws an error.

I would recommend simply setting locator_init_in_progress to undefined. Is there a particular reason you need the property removed? I've found that setting a property to undefined is often safer than using the delete keyword because delete doesn't always behave the way developers think it will.

stinkycheeseman
  • 43,437
  • 7
  • 30
  • 49
  • If you note, I'm *trying* to set it to undefined. It's still throwing the error. – EmmyS Aug 20 '13 at 19:04
  • Even completely getting rid of all if/try-catch stuff and just setting the variable to undefined throws the error. IE8 just really doesn't seem to like `undefined`. – EmmyS Aug 20 '13 at 19:33
  • Hm, perhaps it doesn't like having properties set on "window"? Can you try instantiating another global variable and putting the property on that? – stinkycheeseman Aug 20 '13 at 20:15
  • Unfortunately no. This is a small but frequently-used part of a huge codebase. It works in all other browsers; I can't start changing the variable for one browser. – EmmyS Aug 20 '13 at 20:16
  • And the annoying thing is that the IE8 dev tools doesn't even give me a line number on the thrown error, so I'm guessing at where it's happening. It may not even be the piece I'm dealing with; I just happened to know that IE doesn't like delete so tried to replace it with undefined. It may actually be some other piece of code entirely that's throwing the error. – EmmyS Aug 20 '13 at 20:18