来自IE 8.0中的XMLHttpRequest()的GetElementbyId

时间:2014-02-03 10:57:23

标签: javascript internet-explorer xmlhttprequest youtube-javascript-api

我正在开发YouTube用户历史记录视图功能,现在我正在尝试使用youtubeInfoUri获取YouTube userId并请求令牌,

这里的代码在FF和Chrome中工作得很棒,但在IE中却失败了

function getDataFromXML() {
try {

    youTubeUserInfoUri += acToken; // global variable currently. 
    // i.e youTubeUserInfoUri = "https://gdata...."
    //IE : if URI contain https  then exception thrown  :
    /* description : $lineinfo is undefined */
    /* Name : TypeeEror */        

    //To handle this :
    youTubeUserInfoUri = youTubeUserInfoUri.replace("https", "http");

    var request = new XMLHttpRequest();

    request.open("GET", youTubeUserInfoUri, false);


    request.timeout = 3000;
    request.onprogress = function () { };
    request.ontimeout = function () { };
    request.onerror = function () { };


    request.send();

    if (request.status == 200) {

        var xml = request.responseXML;     

这里是xml:我得到了childnodes = {count 0}; item =参数数量无效

现在我在这里完全空白,不知道为什么会发生这种情况......

        if (xml == null) {
            //if Google account dont have youtube account / error 401 found ,
            showShortNotification("YouTube", "current user is not linked with YouTube", "error");
        } else {

            //Browser exception handle :  
            //TagName understanding is different by FF and & Chrome browser,

            var users = xml.getElementsByTagName("username"); // here check for Chrome:   Firefox will not understand this tag name
            if (users.length == 0) { // if its length is 0 means no elements found, so try for next tag method
                users = xml.getElementsByTagName("yt:username"); // here firefox understand this method


          /* In IE : during debug point : here users = Item : Invalid number of parameters.*/


            }

            for (var i = 0; i < users.length; i++) {
                var youtubeUserid = (users[i].childNodes[0].nodeValue);
                YoutubeUserHistoryFetch(youtubeUserid);
            }
        }
    }
} catch (e) {
    alert("Error found" + e);

   }
 }

我也试过了 [1] new window.XDomainRequest(); [2]尝试更改Internet Explorer可信站点区域设置(Internet Explorer选项 - >安全 - >可信站点+自定义级别,向下滚动并将跨域的访问数据源值更改为启用

Xml响应文本:

 .....<yt:maxUploadDuration seconds="930" /> 
 <yt:statistics lastWebAccess="1970-01-01T00:00:00.000Z" subscriberCount="9" videoWatchCount="0" viewCount="0" totalUploadViews="70" /> 
  <media:thumbnail url="http://yt4.ggpht.com/-pFKkUesgV-Q/AAAAAAAAAAI/AAAAAAAAAAA/I23mZGfUo-A/s88-c-k-no/photo.jpg" /> 
  <yt:username>saun4frsh</yt:username> 

我需要

如果您有任何想法可以解决此问题,请提供帮助。 只有Internet Explorer的问题,相同的代码在Chrome和FF中正常工作。

我有IE8,赢得7 OS。

1 个答案:

答案 0 :(得分:1)

使用此代码:特别适用于IE 8,100%正常工作

function getDataFromXML() {
try {
    youTubeUserInfoUri += acToken;
    if (navigator.userAgent.search("MSIE") >= 0) {
        var rssData = httpGet();
        var youtubeUserid = convertToArrayForIE(rssData.getElementsByTagName('yt:username'));
        YoutubeUserHistoryFetch(youtubeUserid[0]);

    } else {
    //Rest ur code for FF/Chrome Old code
  }
}
catch{}
}

function httpGet() {

youTubeUserInfoUri = youTubeUserInfoUri.replace("https", "http"); //IE not allows https : security issue.
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', youTubeUserInfoUri, false);
xmlHttp.send();

if (window.DOMParser) {
    var parser = new DOMParser();
    var doc = parser.parseFromString(xmlHttp.responseText, 'text/xml');
    return doc;
}
else {
    var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
    xmlDocument.async = false;
    xmlDocument.loadXML(xmlHttp.responseText);
    return xmlDocument;
   }
}

function convertToArrayForIE(htmlCollection) {
var nodes = [];
var collectionLength = htmlCollection.length;
for (var i = 0; i < collectionLength; i++) {
    nodes.push(htmlCollection.item(i).firstChild.text);
}
return nodes;
}