php 推迟解析JavaScript

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 推迟解析JavaScript相关的知识,希望对你有一定的参考价值。

//* OPTION 1 – Deferring parsing of JavaScript via functions.php
//* Alternatively, it is possible to carry out the task by editing one of WordPress core files. To defer parsing of JS, this code should be copied to the bottom of your theme’s functions.php file:

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 );
//* OPTION 2 - Defer Javascripts
//* Defer jQuery Parsing using the HTML5 defer property
if (!(is_admin() )) {
    function defer_parsing_of_js ( $url ) {
        if ( FALSE === strpos( $url, '.js' ) ) return $url;
        if ( strpos( $url, 'jquery.js' ) ) return $url;
        // return "$url' defer ";
        return "$url' defer onload='";
    }
    add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
}
//* OPTION 3 – Deferring parsing of JavaScript via functions.php

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 );

以上是关于php 推迟解析JavaScript的主要内容,如果未能解决你的问题,请参考以下文章

php 推迟解析JavaScript

php 推迟解析JS以加速WordPress网站。

php 使用HTML5 defer属性推迟jQuery解析

延迟解析 JavaScript 不起作用

javascript 推迟加载javascript

javascript jquery推迟