"Null" is null or not an object error in IE javascript

rossmcm picture rossmcm · May 25, 2010 · Viewed 12.3k times · Source

The following code executes fine in Firefox and Chrome, but gives an error:

'null' is null or not an object

when executed in Internet Explorer.

if (xmlhttp.responseXML != null)
    {
    var xmlDoc = xmlhttp.responseXML.documentElement ;
    var ResultNodes = xmlDoc.getElementsByTagName ("Result") ;   <---- error here
    if (ResultNodes != null)
        {

(I would have thought the line after the one indicated would be more likely to return the error but the debugger says the run-time error is at the line indicated)

Any ideas why?

Answer

jeanreis picture jeanreis · May 25, 2010

Try something like this (as usual, IE does things diferently) (take from http://msdn.microsoft.com/en-us/library/ms534370(VS.85).aspx )

if (xmlhttp.responseXML.xml)
    var xmlDoc = xmlhttp.responseXML.xml;
else
    var xmlDoc = xmlhttp.responseXML;