有什么方法可以在不阻止主线程的情况下运行Google Ads代码?Google Pagespeed Insights向我显示了“减少第三方代码的影响”警告:第三方代码阻止了主线程...
Third-Party Size Main-Thread Blocking Time
Google/Doubleclick Ads 193 KB 253 ms
我已经在页脚的页面末尾放置了一个脚本。
<script data-ad-client="ca-pub-xxx" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
我试图添加“ data-aload data-original = ...”,但这无济于事。使用requestAnimationFrame()或setTimeOut()也许是一个正确的选择,但是我不知道如何在此实现它。
您可以动态添加脚本。注意,async
由于浏览器默认认为所有动态脚本都是异步的,因此无需添加
const loadScript = (src, id, callback) => {
const script = document.createElement('script');
script.src = src; // URL for the third-party library being loaded.
script.id = id; // e.g., googleMaps or stripe
script.defer = true; // make sure that browser will run script after page loaded
document.body.appendChild(script);
script.onload = () => {
if (callback) callback(); // conditional callback
};
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句