1

是否有解决方案在页面加载后加载谷歌脚本,因此谷歌页面速度指数没有受到影响?

4

1 回答 1

1

此代码对繁重的 javascript 代码设置了超时,如果用户滚动它也会运行,因此内容会显示得更快

var scrollCounter = false;
var pageType = typeof signal !== 'undefined' && signal.Content.ArticleId!=null?'article':'notanarticle';
var cds = /Mobi/.test(navigator.userAgent) && document.body.clientWidth < 737 ? 'Mobile' : /Mobi/.test(navigator.userAgent) && document.body.clientWidth > 767 ? 'Tablet' : 'Desktop';
if (cds == 'Mobile') {
    //condition for home and section pages
      if(pageType == 'notanarticle') {
       setTimeout(function() {
            if (!scrollCounter) {
                scrollCounter = true;
                console.log('timeout');
                addThirdPartyLibs();
            }
        }, 4000);   
     }//end of condition
    var activityEvents = [
        'keydown', 'touchstart', 'scroll'
    ];
    activityEvents.forEach(function(eventName) {
        document.addEventListener(eventName, function(e) {
            if (!scrollCounter) {
                scrollCounter = true;
                console.log(window.scrollY);
                addThirdPartyLibs();
            }
        }, true);
    });
} else {
    addThirdPartyLibs();
}

function addThirdPartyLibs() {
    loadGPTJs();

}
于 2022-01-07T15:24:23.870 回答