0

我的最终目标是有一个主页,它可以提取 RSS 提要并将它们显示在我的个人网站主页上。这一切都是从客户端完成的,我不打算构建一个服务器站点脚本来做到这一点(尽管只有在没有其他方法可行的情况下我才会这样做)。

下面是页面加载代码。(我不认为这是一个问题领域,但很高兴看到起点)

 $(document).ready(function () { // Here
    loadpageF(); //<- problems is in this function 
    GetBackGround(document.body); //this just messes with the css of the site
});

function loadpageF() {
    addTableFeed("http://www.npr.org/rss/rss.php?id=1001");
    addTableFeed("http://news.yahoo.com/rss/odd");
    addTableFeed("http://www.nfl.com/rss/rsslanding?searchString=team&abbr=DAL");
    addTableFeed("http://www.npr.org/rss/rss.php?id=1007");


    $(".MainArea").css("width",$("iframe").length * (270));

}
//This function sets up where the RSS data will be place after formatting
 function addTableFeed(feedlink) {

    var numOfCell = maintb.getElementsByTagName("iframe").length;


    maintb.innerHTML += "<iframe id='f" + numOfCell + "' class='iframeFeed'></iframe>";

    getRSSFeed(feedlink, "f" + numOfCell);

}

这段代码基本上为每个 rss 调用一个函数来添加和 iframe,并将 iframe id 和 URL 传递给另一个获取数据的函数(如下所示,我 99% 肯定是问题所在)。

 function getRSSFeed(feedlink, iframeid) {


     /*var xmlhttp; old way I did it below realizing it did not work in firefox

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
   xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            var hold = xmlhttp.responseText
            loadData(hold, iframeid);
        }
    }
    xmlhttp.open("get", feedlink, false);
    xmlhttp.send();*/ //end of the old way

    //new way to try to use JSON of course failing at my in goal
    $.ajax({
        type: "GET",
        url: feedlink,
        async: true,
        dataType: "text",   

        success: function(data){
            loadData(data, iframeid);
        },
        error: function(data, statusCode) {
            alert("ERROR: "+data.error)
        }

    });}

函数 loadData 在调用时可以正常工作,但不会为 Yahoo.com 和 NFL.com 提要调用。相反,我收到如下所示的警报。

    //Note I has to transcribe from dialog box there may be errors
    ERROR: function () { 
    if( list ) {
        // First, we save the current length
        var start = list length;
        (function add( args ) {
            jQuery.each( args, function( _, arg ) {
                ¡f( jQuery.isFunction( arg ) && (!options unique || self.has( arg)
                    list .push( arg);
                else if( arg  && arg.length ) {
                    // Inspect recursively
                    add( arg);
                }
                });
            })( arguments);
            //Do we need to add the callbacks to the
            // current firing batch?
            if (firing ) (
                firingLength = list length;
            //wiIh memory, if we’re not firing then
            // we should call right away
            J else if ( memory ) (
                firingstart = start;
                fire( memory);

以防万一,下面是加载数据代码。

function loadData(xml, ifid) {
    var htmlStr;
    var artCount = 0;

    var $xml = $($.parseXML(xml));
    $("#" + ifid)[0].contentDocument.open();
    $("#" + ifid)[0].contentDocument.close();
    $("#" + ifid)[0].contentDocument.write("<head><link rel='Stylesheet' type='text/css' href='" + document.getElementById("StyleSheet").href + "' /></head>");
    //RSS 2.0 handler
  $xml.find("channel item").each(function () {

        var $article = $(this);
        var title = $article.find("title").text();
        var description = $article.find("description").text();
        var link = $article.find("link").text();
        var pubDate = $article.find("pubDate").text();
        htmlStr = "<div id='" + artCount + "Span" + ifid + "' class='ArticalHead'><h3>" + title + "</h3></div>\n";
        htmlStr += "<div class='ArticalBody' id='" + artCount + "Div" + ifid + "'>\n";
        htmlStr += "\t<p>" + description + "</p><a href='" + link + "' target='_blank' > Click Here For more </a>\n";
        htmlStr += "\t<h6>" + pubDate + "</h6><br />\n";
        htmlStr += "</div>\n"


        $("#" + ifid)[0].contentDocument.write(htmlStr);

         $("#" + ifid).contents().find("#" +artCount + "Span" + ifid).click(function () {
             $("#" + ifid).contents().find("#" + $(this).attr("id").replace("Span", "Div")).toggle();
        });
        artCount++;
    });

    //Atom 1.0 handler
    $xml.find("feed entry").each(function () {

        var $article = $(this);
        var title = $article.find("title").text();
        var description = $article.find("summary").text();
        var link = $article.find("link").attr("href");
        var pubDate = $article.find("published").text();
        htmlStr = "<div id='" + artCount + "Span" + ifid + "' class='ArticalHead'><h3>" + title + "</h3></div>\n";
        htmlStr += "<div class='ArticalBody' id='" + artCount + "Div" + ifid + "'>\n";
        htmlStr += "\t<p>" + description + "</p><a href='" + link + "' target='_blank' > Click Here For more </a>\n";
        htmlStr += "\t<h6>" + pubDate + "</h6><br />\n";
        htmlStr += "</div>\n"


        $("#" + ifid)[0].contentDocument.write(htmlStr);

        $("#" + ifid).contents().find("#" + artCount + "Span" + ifid).click(function () {
            $("#" + ifid).contents().find("#" + $(this).attr("id").replace("Span", "Div")).toggle();
        });
        artCount++;
    });
    //$("#" + ifid).contents().find(".ArticalBody").hide();

}

请原谅我的任何拼写错误或拼写错误。如果需要,我也可以发布整个网页。

4

0 回答 0