简体   繁体   English

如何推迟或异步执行此JS脚本代码?

[英]How can I defer or async this JS script code?

I have a problem with this code. 我对此代码有疑问。 I know that we can add async or defer for external scripts, but with this script I can't handle with the document.write("<script src=\\"http://example...... , so when I add defer or async to that code the script stop working. Can anyone help me because this code made some render blocking problem with my blog. 我知道我们可以为外部脚本添加异步或延迟,但是使用此脚本我无法处理document.write("<script src=\\"http://example...... ,所以当我在该代码中添加延迟或异步脚本会停止工作,谁能帮我,因为此代码对我的博客造成了一些渲染阻止问题。

This is the code: 这是代码:

 <script type='text/javascript'> var numposts = 5; var showpostthumbnails = true; var showpostdate = false;</script> <script type='text/javascript'> //<![CDATA[ // Recent Post By Tag // Recent Post By Tag For Blogger function rcentbytag(e){document.write('<ul class="recent-by-tag">');for(var t=0;t<numposts;t++){var n=e.feed.entry[t];var r=n.title.$t;var i;if(t==e.feed.entry.length)break;for(var o=0;o<n.link.length;o++){if(n.link[o].rel=="replies"&&n.link[o].type=="text/html"){var u=n.link[o].title;var f=n.link[o].href}if(n.link[o].rel=="alternate"){i=n.link[o].href;break}}var l;try{l=n.media$thumbnail.url}catch(h){s=n.content.$t;a=s.indexOf("<img");b=s.indexOf('src="',a);c=s.indexOf('"',b+5);d=s.substr(b+5,cb-5);if(a!=-1&&b!=-1&&c!=-1&&d!=""){l=d}else l="http://2.bp.blogspot.com/-giova1ZCh-A/Uzq6L8QTJNI/AAAAAAAAAJc/USXictTq_xs/s70-c/KM+Icon.png"}var p=n.published.$t;var v=p.substring(0,4);var m=p.substring(5,7);var g=p.substring(8,10);var y=new Array;y[1]="Januari";y[2]="Februari";y[3]="Maret";y[4]="April";y[5]="Mei";y[6]="Juni";y[7]="Juli";y[8]="Agustus";y[9]="September";y[10]="Oktober";y[11]="November";y[12]="Desember";document.write('<li class="clear">');if(showpostthumbnails==true)document.write('<a href="'+i+'" target ="_blank" title="'+r+'"><img class="rct-thumb" alt="'+r+'" src="'+l+'"/></a>');document.write('<strong><a href="'+i+'" target ="_blank" title="'+r+'">'+r+'</a></strong>');document.write('<br>');var x="";var T=0;if(showpostdate==true){x='<span class="showdates">'+x+g+" "+y[parseInt(m,10)]+" "+v+"</span>";T=1}document.write(x);document.write("</li>");if(t!=numposts-1)document.write("")}document.write("</ul>")} //]]> </script> <script> document.write("<script src=\\"http://example.blogspot.com/feeds/posts/default/-/LabelHere?orderby=published&alt=json-in-script&callback=rcentbytag\\"><\\/script>"); </script> 

Thank You ... 谢谢 ...

defer or async change the load timing of your scripts and thus will change when your script executes and thus will change the timing of the document.write() statements in your rcentbytag() function. deferasync更改脚本的加载时间,因此将在脚本执行时更改,因此将更改rcentbytag()函数中document.write()语句的时间。

Since the rcentbytag() function is inserting content into your page with document.write() , you can't change the location of that script in the page unless you rewrite it not to use document.write() and to insert content into the appropriate place in the page another way (such as .appendChild() ). 由于rcentbytag()函数使用document.write()将内容插入到页面中,因此除非更改脚本以不使用document.write()并将内容插入到页面中,否则无法更改该脚本在页面中的位置。以另一种方式在页面中的适当位置(例如.appendChild() )。

So, if you want to defer the loading of the script that calls rcentbytag() , then you will have to rewrite rcentbytag() to insert the content it puts into the page a different way that does not use document.write() . 因此,如果要推迟调用rcentbytag()的脚本的加载,则必须重写rcentbytag()才能以不使用document.write()的另一种方式将其放入页面中。

You will have a lot more options when you remove all use of document.write() . 删除所有对document.write()使用时,您将有更多选择。 Besides getting in your way here, it can also slow down the loader for a variety of reasons. 除了妨碍您的工作之外,由于各种原因,它还会使加载程序变慢。

Your script that uses document.write() works out to be this when it's decompressed: 解压缩后使用document.write()脚本就是这样的:

function rcentbytag(e) {
    document.write('<ul class="recent-by-tag">');
    for (var t = 0; t < numposts; t++) {
        var n = e.feed.entry[t];
        var r = n.title.$t;
        var i;
        if (t == e.feed.entry.length) break;
        for (var o = 0; o < n.link.length; o++) {
            if (n.link[o].rel == "replies" && n.link[o].type == "text/html") {
                var u = n.link[o].title;
                var f = n.link[o].href
            }
            if (n.link[o].rel == "alternate") {
                i = n.link[o].href;
                break
            }
        }
        var l;
        try {
            l = n.media$thumbnail.url
        } catch (h) {
            s = n.content.$t;
            a = s.indexOf("<img");
            b = s.indexOf('src="', a);
            c = s.indexOf('"', b + 5);
            d = s.substr(b + 5, c - b - 5);
            if (a != -1 && b != -1 && c != -1 && d != "") {
                l = d
            } else l = "http://2.bp.blogspot.com/-giova1ZCh-A/Uzq6L8QTJNI/AAAAAAAAAJc/USXictTq_xs/s70-c/KM+Icon.png"
        }
        var p = n.published.$t;
        var v = p.substring(0, 4);
        var m = p.substring(5, 7);
        var g = p.substring(8, 10);
        var y = new Array;
        y[1] = "Januari";
        y[2] = "Februari";
        y[3] = "Maret";
        y[4] = "April";
        y[5] = "Mei";
        y[6] = "Juni";
        y[7] = "Juli";
        y[8] = "Agustus";
        y[9] = "September";
        y[10] = "Oktober";
        y[11] = "November";
        y[12] = "Desember";
        document.write('<li class="clear">');
        if (showpostthumbnails == true) document.write('<a href="' + i + '" target ="_blank" title="' + r + '"><img class="rct-thumb" alt="' + r + '" src="' + l + '"/></a>');
        document.write('<strong><a href="' + i + '" target ="_blank" title="' + r + '">' + r + '</a></strong>');
        document.write('<br>');
        var x = "";
        var T = 0;
        if (showpostdate == true) {
            x = '<span class="showdates">' + x + g + " " + y[parseInt(m, 10)] + " " + v + "</span>";
            T = 1
        }
        document.write(x);
        document.write("</li>");
        if (t != numposts - 1) document.write("")
    }
    document.write("</ul>")
}

It is possible to replace all the document.write() calls in that with a new function that just accumulates the HTML and then insert all that HTML once at the end into a previously placed div in your document: 可以用一个新函数替换所有的document.write()调用,该函数仅会累积HTML,然后将所有HTML末尾插入到文档中先前放置的div中:

function rcentbytag(e) {
    var storedHTML = "";

    function saveHTML(h) {
        storedHTML += h;
    }

    saveHTML('<ul class="recent-by-tag">');
    for (var t = 0; t < numposts; t++) {
        var n = e.feed.entry[t];
        var r = n.title.$t;
        var i;
        if (t == e.feed.entry.length) break;
        for (var o = 0; o < n.link.length; o++) {
            if (n.link[o].rel == "replies" && n.link[o].type == "text/html") {
                var u = n.link[o].title;
                var f = n.link[o].href
            }
            if (n.link[o].rel == "alternate") {
                i = n.link[o].href;
                break
            }
        }
        var l;
        try {
            l = n.media$thumbnail.url
        } catch (h) {
            s = n.content.$t;
            a = s.indexOf("<img");
            b = s.indexOf('src="', a);
            c = s.indexOf('"', b + 5);
            d = s.substr(b + 5, c - b - 5);
            if (a != -1 && b != -1 && c != -1 && d != "") {
                l = d
            } else l = "http://2.bp.blogspot.com/-giova1ZCh-A/Uzq6L8QTJNI/AAAAAAAAAJc/USXictTq_xs/s70-c/KM+Icon.png"
        }
        var p = n.published.$t;
        var v = p.substring(0, 4);
        var m = p.substring(5, 7);
        var g = p.substring(8, 10);
        var y = new Array;
        y[1] = "Januari";
        y[2] = "Februari";
        y[3] = "Maret";
        y[4] = "April";
        y[5] = "Mei";
        y[6] = "Juni";
        y[7] = "Juli";
        y[8] = "Agustus";
        y[9] = "September";
        y[10] = "Oktober";
        y[11] = "November";
        y[12] = "Desember";
        saveHTML('<li class="clear">');
        if (showpostthumbnails == true) saveHTML('<a href="' + i + '" target ="_blank" title="' + r + '"><img class="rct-thumb" alt="' + r + '" src="' + l + '"/></a>');
        saveHTML('<strong><a href="' + i + '" target ="_blank" title="' + r + '">' + r + '</a></strong>');
        saveHTML('<br>');
        var x = "";
        var T = 0;
        if (showpostdate == true) {
            x = '<span class="showdates">' + x + g + " " + y[parseInt(m, 10)] + " " + v + "</span>";
            T = 1
        }
        saveHTML(x);
        saveHTML("</li>");
        if (t != numposts - 1) saveHTML("")
    }
    saveHTML("</ul>")
    document.getElementById("rcentDiv").innerHTML = storedHTML;
}

Then, you can change your HTML to this. 然后,您可以将HTML更改为此。 This inserts a placeholder <div> which is a place for the content to be added and you used the revised rcentbytag() that inserts the HTML into that div and then you can use defer on the feeds script and it will call rcentbytag() which will insert the content appropriately: 这将插入一个占位符<div> ,该占位符是要添加内容的位置,并且您使用了将HTML插入该div的修改后的rcentbytag() ,然后可以在feed脚本上使用defer ,它将调用rcentbytag() ,将适当地插入内容:

<div id="rcentDiv"></div>

<script type='text/javascript'>
var numposts = 5;
var showpostthumbnails = true;
var showpostdate = false;

// Recent Post By Tag
// Recent Post By Tag For Blogger
// insert revised rcentbytag() code here
</script>

<script defer src="http://example.blogspot.com/feeds/posts/default/-/LabelHere?orderby=published&alt=json-in-script&callback=rcentbytag"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM