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";
    if (window.addEventListener)
        window.addEventListener("load", downloadJSAtOnload, false);
    else if (window.attachEvent)
        window.attachEvent("onload", downloadJSAtOnload);
    else window.onload = downloadJSAtOnload;

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

1 Answer 1


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);
add_action( 'wp_enqueue_scripts','enqueue_scripts' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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