0

The best theme in WordPress theme development requires a standard protocol of enqueuing scripts through wp_enqueue_script, but this doesn't solve the problem and Google PageSpeed suffers. I found a solution given by a blogger here:

Script to call external javascript file

This code should be placed in your HTML just before the </body> tag (near the bottom of your HTML file). I highlighted the name of the external JS file.

<script type="text/javascript">
    function downloadJSAtOnload() {
        var element = document.createElement("script");
        element.src = "defer.js";
        document.body.appendChild(element);
    }
    if (window.addEventListener)
        window.addEventListener("load", downloadJSAtOnload, false);
    else if (window.attachEvent)
        window.attachEvent("onload", downloadJSAtOnload);
    else window.onload = downloadJSAtOnload;
</script>

But that will not use wp_enqueue_script. Is there any solution to do this by using wp_enqueue_script?

0

You can copy this code to a JS file then enqueue that to wordpress .

For Ex :

Copy codes to scripts.js in js folder , then in functions.php write :

function enqueue_scripts() { 
    wp_register_script('scripts', get_template_directory_uri(). '/js/scripts.js','', '', true);
    wp_enqueue_script('scripts');
}
add_action( 'wp_enqueue_scripts','enqueue_scripts' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.