Solved: IE event – “this.parentNode.parentNode is null or not an object”

… it bloody-well is! This post was very nearly “Things that piss me off 5: Internet Explorer” (that one may yet come) but I thought a more descriptive title would help more people struggling with the same problem to find it.

Anyway: background.  I am writing a new feature for a Content Management System to allow users to upload files. With a simple bit of javascript I added a “+” button to allow them to add new rows to the form, allowing them to upload multiple files. It looks a bit like this:

Screenshot of Upload Images feature
Screenshot of Upload Images feature

Anyway, it would obviously be nice to allow the user to remove a row if they’ve added one or two too many. The obvious solution seems to be to add a “-”  button beside each. Easy, right?

Unfortunately Internet Explorer, as per usual, ballses everything up. Because each – button is created dynamically I have to add an EventListener using JavaScript code. No problem so far. That event listener is a simple, one-line:

this.parentNode.parentNode.removeChild(this.parentNode);

As each row (the label, field itself (including button) and “-” link) is inside a div, the above code should remove that div and therefore remove the row. And it does… in Firefox. Try it in Internet Explorer and you get a message telling you “Error: ‘this.parentNode.parentNode’ is null or not an object”.

Whhhaaa?!

Don’t you just hate when you can easily find something your code claims it can’t? Am I the only one who screams “It’s right f**king there!!” at my monitor? In this case, the problem seems to boil down to IE’s supid, proprietary, fucked up way of attaching Event listeners. When you use Mozillas the W3C standard addEventListener method to attach a listener to an event, “this” in the event handler refers to the object which the event is called on. Obvious, yes?  However when you use IE’s attachEvent instead, IE doesn’t seem to act in the same way. Apparently, instead, IE sets “this” in an event handler to the window object instead of the calling/event object! Another good job from Micro$oft!

Apparently it’s can be overcome, but that involves a lot of code and I refuse in principle to do that much inelegant hacking for something so simple. It’s much easier (and principled) to do this:

IE error message: "Sorry, your browser doesn't support this function. This is probably because you're using Internet Explorer. www.getFirefox.com"
Not really a long-term solution, but bloody satisfying nonetheless.

Microsoft Want to Own You

Installing Microsoft’s Virtual Earth 3D plugin for Firefox (yes, they wrote something that works in Firefox) I was reminded of one more reason why I hate Microsoft.

You’d think with all the anti-competitive suits being filed against them they’d get over this, but apparently not.

Have a look at what’s presented to you when you install the Microsoft Virtual Earth 3D plugin.
Microsoft Want to Own Your PC

Yes, that’s right, when you install Virtual Earth 3D, Microsoft try to take over your home page and default search engine: two completely unrelated services. If they must give us the option then fine, but they have the boxes ticked by default and that is where I believe they cross the line from opportunistic cross-selling to scummy, underhanded manipulation. Not that it’s a big surprise, they do the same thing with their Windows Live Messenger product. This is an even more blatant example of what they’re trying to do here, which is using their strength (and in the case of Messenger, dominance) in one market to try and dominate in another.

If it takes another legal case to sort this out then it should be done, but Microsoft should be fined and forced to pay legal costs. The previous controversy over their bundling of Media Player in Windows obviously hasn’t taught them anything.