Mike McKee
Mike McKee

Reputation:

IE6 and IE7 Sometimes Have A jQuery Bug With 'nodeName' is null or not an object

I'm having a problem where a jQuery setting against an .html() property on a selected element is returning the error 'nodeName' is null or not an object. This only occurs on IE6 and IE7, but not FF2, FF3, Opera (latest Nov 12,2008) or Safari (again, latest).

Upvotes: 2

Views: 8555

Answers (5)

Mohd Farid
Mohd Farid

Reputation: 2070

For me it was happening on IE when I was trying to select an element that did not existed. I was trying to get its index amongst its siblings which was returned as -1. Then I tried showing this element by getting it by index from its parent.It resulted in this error.

Thus, I put a check whether the index is not equal to -1. This solved the issue for me.

Upvotes: 1

Oscar
Oscar

Reputation: 1

It looks to me like a bug in JQuery. The exception is thrown at line 605 in 1.5.1:

nodeName: function( elem, name ) {
    return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
},

The function returns true if the nodeName of object elem is identical to the String name. If not, or if there is no nodeName at elem, we return false. However, elem is not tested before it is used. So if elem is null, the invocation of its .nodeName member throws a null pointer exception.

A simple fix is to include elem at the beginning of the short-circuit AND clause:

return elem && elem.nodeName && elem.nodeName.toUpperCase()...

Now if elem is null, the function will return false at the first test in the clause and never try to invoke elem.nodeName, thus avoiding the NPE.

I didn't check them all (it's used a lot), but in many cases where this function is used, elem is tested before the function call. But not in all cases, apparently.

Upvotes: 2

Rob
Rob

Reputation: 3516

Do you have any idea what kinds of nodes you might be running up against? Or, are you running in IE quirks mode? There might be some kinds of nodes such as #text that don't show up correctly in the DOM in quirks mode.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Upvotes: 0

The Archetypal Paul
The Archetypal Paul

Reputation: 41769

I don't know if it's connected, but we've had what sounds like a similar issue where the DOM doesn't have the children/text of a element that we know exist (because we see them rendered on the screen!)

Selecting something else, then selecting the elememt again seemed to fix the issue - suddenly, the children appear. So what happens if you select you element, select something else, select your element again?

Upvotes: 0

Mike McKee
Mike McKee

Reputation:

I resolved the problem. The example looks like this:

$('#section #detail .data').html(data);

...where data is HTML returned from an AJAX call, and this bug only occurs on IE6 and IE7 on the second attempt AJAX call, not the first. It's inexplicable. The error returned is:

'nodeName' is null or not an object

The fix is to simply clear the variable first before setting:

$('#section #detail .data').html(''); $('#section #detail .data').html(data);

And then IE6 and IE7 started working again with it.

BTW, I had to install Visual Web Developer 2008 Express Edition to get a debugger working in IE7. That info is here.

Upvotes: 2

Related Questions