我想手动消除渲染阻塞以提高速度。
过去两天,我一直在谷歌搜索解决方案。
在功能方面尝试了以下3种解决方案。php。然而,这确实奏效了。。。。
从一半的js脚本中删除了,我仍然能收到其他js的相同消息没有从CSS中消除
Solution 1
add_filter( \'clean_url\', function( $url )
{
if ( FALSE === strpos( $url, \'.js\' ) )
{
return $url;
}
return "$url\' defer=\'defer";
}, 11, 1 );
Solutions 2 given hereadd_action(\'customize_register\', \'customizer\');
function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, \'.js\' ) ) return $url;
if ( strpos( $url, \'jquery.js\' ) ) return $url;
return "$url\' defer ";
}
add_filter( \'clean_url\', \'defer_parsing_of_js\', 11, 1 );
Solutions 3
// add async and defer to javascripts
function wcs_defer_javascripts ( $url ) {
if ( FALSE === strpos( $url, \'.js\' ) ) return $url;
if ( strpos( $url, \'jquery.js\' ) ) return $url;
return "$url\' async=\'async";
}
add_filter( \'clean_url\', \'wcs_defer_javascripts\', 11, 1 );
请不要推荐任何插件。我尝试过w3缓存、自动优化等等。
W3缓存完成了任务,但这不是永久解决方案。因为一旦w3缓存停用,站点就会变形(site does not return to normal even after reactivating).
Solution 4 之前添加了此代码
<script type="text/javascript">
function downloadJSAtOnload() {
var links = ["wp-content/themes/TL/library/js/scriptsslider.js", "wp-content/themes/TL/library/js/scriptsslider.js"],
headElement = document.getElementsByTagName("head")[0],
linkElement, i;
for (i = 0; i < links.length; i++) {
linkElement = document.createElement("script");
linkElement.src = links[i];
headElement.appendChild(linkElement);
}
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>