IE中的Javascript错误(getElementsByTagName(...)。item(...)'为null或不是对象)

时间:2011-08-05 08:23:22

标签: javascript internet-explorer getelementsbytagname

想要使用Accuweather花粉计数谷歌应用程序,但它在IE中抛出错误。

以下是直接脚本的链接:

  

http://www-igprev-opensocial.googleusercontent.com/gadgets/ifr?exp_rpc_js=1&exp_track_js=1&url=http%3A%2F%2Fwhapowllc.googlepages.com%2Fpollen.xml&container=igprev&view=default&lang=en&country=US&sanitize=0&v=45408d622dfd6df4&parent=http://www.google.com&libs=core:core.io:core.iglegacy:auth-refresh&is_signedin=1&synd=igprev&view=default

以下是有问题的环绕声脚本:

        function setContent(url) {
        _IG_FetchXmlContent(url, function (response) {
            if (!response) {
                msg.createDismissibleMessage("Error loading location data. Please try again later.");
                echoDefault();
            } else if (response.firstChild.nodeName != 'error' &&
                parseInt(response.getElementsByTagName('citylist').item(0).getAttribute('us'))
                + parseInt(response.getElementsByTagName('citylist').item(0).getAttribute('intl'))
                + parseInt(response.getElementsByTagName('citylist').item(0).getAttribute('extra_cities'))) {
                mylocation = response.getElementsByTagName('location').item(0).getAttribute('location');
                url = 'http://whapw.accu-weather.com/widget/whapw/weather-data.asp?location=' + mylocation;

                _IG_FetchXmlContent(url, function (response) {
                    if (!response) {
                        msg.createDismissibleMessage("Error loading pollen data. Please try again later.");
                        echoDefault();
                    } else {
                        var tree = response.getElementsByTagName('tree').item(0).firstChild.nodeValue;
                        var weed = response.getElementsByTagName('weed').item(0).firstChild.nodeValue;
                        var grass = response.getElementsByTagName('grass').item(0).firstChild.nodeValue;
                        var mold = response.getElementsByTagName('mold').item(0).firstChild.nodeValue;
                        var airquality = response.getElementsByTagName('airquality').item(0).firstChild.nodeValue;
                        var airqualitytype = response.getElementsByTagName('airqualitytype').item(0).firstChild.nodeValue;
                        var airqualitystyle = '';

此行报告错误:

    } else if (response.firstChild.nodeName != 'error' &&

在FF / Chrome中运行良好,但在IE中我得到了:

  

网页错误详情

     

用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 6.1; Trident / 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0 ; InfoPath.2; 3M / MSIE 8.0)   时间戳:星期五,2011年8月5日08:01:57 UTC

Message: 'getElementsByTagName(...).item(...)' is null or not an object
Line: 986
Char: 12
Code: 0
URI: http://www-igprev-opensocial.googleusercontent.com/gadgets/ifr?exp_rpc_js=1&exp_track_js=1&url=http%3A%2F%2Fwhapowllc.googlepages.com%2Fpollen.xml&container=igprev&view=default&lang=en&country=US&sanitize=0&v=45408d622dfd6df4&parent=http://www.google.com&libs=core:core.io:core.iglegacy:auth-refresh&is_signedin=1&synd=igprev&view=default

任何想法导致此错误的原因是什么?

提前致谢。

1 个答案:

答案 0 :(得分:1)

在IE的某些版本中有一些奇怪的地方,其中 item(0)返回NodeList的长度,而不是第一项。相反,只需使用索引访问项目:

...getElementsByTagName('...')[0];