document.write()在页面加载后调用时删除所有页面内容

时间:2016-01-15 14:59:49

标签: javascript json blogger document.write blogspot

我有一个网页,我正在使用JSON从他的Feed中阅读Google Blogger博客类别。我有两个功能。首先是获取所有博客类别列表,第二个是从中获取博客类别,然后再次点击博客以获取最新帖子这是文本测试,以查看网页数据是否在这里。

<div id="blogCategoriesList">
<script type="text/javascript">
var blogurl = "https://googleblog.blogspot.com/";
function blogCatList(json) {
    document.write('<select onchange="showThisCatPosts(this.value)">');
    document.write('<option>CHOOSE A CATEGORY</option>');
    for (var i = 0; i < json.feed.category.length; i++)
    {
        var item = "<option value='" + json.feed.category[i].term + "'>" + json.feed.category[i].term + "</option>";
        document.write(item);
    }
    document.write('</select>');
}                   
document.write('<script type=\"text/javascript\" src=\"' + blogurl + '/feeds/posts/default?redirect=false&orderby=published&alt=json-in-script&callback=blogCatList&max-results=500\"><\/script>');
document.write('<br/><br/><a href=\"' + blogurl + '\" target=\"_blank\" class=\"footerLINK\">Read The Blog Online Now<\/a>');
</script>
</div>
<div id="blogCategoriesPost">
<script style='text/javascript'>  
var blogurl = "https://googleblog.blogspot.com/";
var numposts = 10;  // Out Of 500
var displaymore = true;
var showcommentnum = true;
var showpostdate = true;
var showpostsummary = true;
var numchars = 100;
function blogCategoriesPost(json) {                  
    if(json.feed.entry.length < numposts ){
        numposts = json.feed.entry.length;
    }
    for (var i = 0; i < numposts; i++) {
        var entry = json.feed.entry[i];
        var posttitle = entry.title.$t;
        var posturl;
        if (i == json.feed.entry.length) break;
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
                var commenttext = entry.link[k].title;
                var commenturl = entry.link[k].href;
            }
            if (entry.link[k].rel == 'alternate') {
                posturl = entry.link[k].href;
                break;
            }
        }                        
        var postdate = entry.published.$t;
        var cdyear = postdate.substring(0, 4);
        var cdmonth = postdate.substring(5, 7);
        var cdday = postdate.substring(8, 10);
        var monthnames = new Array();
        monthnames[1] = "Jan";
        monthnames[2] = "Feb";
        monthnames[3] = "Mar";
        monthnames[4] = "Apr";
        monthnames[5] = "May";
        monthnames[6] = "Jun";
        monthnames[7] = "Jul";
        monthnames[8] = "Aug";
        monthnames[9] = "Sep";
        monthnames[10] = "Oct";
        monthnames[11] = "Nov";
        monthnames[12] = "Dec";
        document.write('<div id="mainDIV">');                        
        document.write('<h2 class="post_heading">' + posttitle + '</h2>');
        if ("content" in entry) {
            var postcontent = entry.content.$t;
        } else
            if ("summary" in entry) {
                var postcontent = entry.summary.$t;
            } else var postcontent = "";
        var re = /<\S[^>]*>/g;
        postcontent = postcontent.replace(re, "");   // Will Show Only Text Instead Of HTML
        if (showpostsummary == true) {
            if (postcontent.length < numchars) {
                document.write('<span class="post_summary">');
                document.write(postcontent);
                document.write('</span>');
            } else {
                //document.getElementById("catPosts").innerHTML += '<span class="post_summary">';
                document.write('<span class="post_summary">');
                postcontent = postcontent.substring(0, numchars);
                var quoteEnd = postcontent.lastIndexOf(" ");
                postcontent = postcontent.substring(0, quoteEnd);
                document.write(postcontent + '...');
                document.write('</span>');
            }
        }
        var towrite = '';
        document.write('<strong class="post_footer">');
        if (showpostdate == true) {
            towrite = 'Published On: ' + towrite + monthnames[parseInt(cdmonth, 10)] + '-' + cdday + '-' + cdyear;
        }
        if (showcommentnum == true) {
            if (commenttext == '1 Comments') commenttext = '1 Comment';
            if (commenttext == '0 Comments') commenttext = 'No Comments';
            commenttext = '<br/><a href="' + commenturl + '" target ="_blank">' + commenttext + '</a>';
            towrite = towrite + commenttext;
        }
        if (displaymore == true) {
            towrite = towrite + '<br/><a href="' + posturl + '" target="_blank">Read Full Article --></a>';
        }
        document.write(towrite);
        document.write('</strong></div>');
    }                    
}
function showThisCatPosts(BLOGCAT){
    document.write('<script type=\"text/javascript\" src=\"' + blogurl + '/feeds/posts/default/-/' + BLOGCAT + '?redirect=false&orderby=published&alt=json-in-script&callback=blogCategoriesPost&max-results=500\"><\/script>');
    document.write('<a href=\"' + blogurl + '\" target=\"_blank\" class=\"footerLINK\">Read The Blog Online Now<\/a>');
}
</script>

您可以在JSBIN看到有效的DEMO。我的问题是,当页面加载时,它的工作完美地显示了所有页面数据和博客类别列表,但是当我选择任何类别时,我的所有页面数据都被移除,只有那些标签帖子可见。为什么会这样...... ???我想根据标签更改帖子,而不是删除所有页面数据......

2 个答案:

答案 0 :(得分:1)

这是document.write()的正常行为。您可能需要使用:

document.getElementById("element_id").innerHTML = 'Stuff to Write.';

答案 1 :(得分:0)

最后我解决了。原因是document.write()在页面加载时写入文本是不好的。如果您想在页面加载时写一些文本,可以使用它。

如果你想稍后写,那么必须使用document.getElementById("element_id").innerHTML来编写你的文字,但是如果你想写<script>个标签,那么document.getElementById("element_id").innerHTML并不好,因为它会写但不会打SRC因此在页面加载后使用document.createElement("script")编写脚本也可以运行。

JSBIN ...查看我的代码的正常工作DEMO:)

特别感谢: @Praveen Kumar : - )