0

When not in WordPress we can tell a script to load when the entire page has loaded like this :

$(window).bind("load", function() {
   // code here
});

But is this achievable through this function (when we use this function):

wp_register_script

Additional information

This is how currently i am enqueuing →

function custom_theme_js() {
    wp_register_script(
        'infinite_scroll',
        get_template_directory_uri() . '/js/infinite-scroll.pkgd.min.js',
        array('jquery'),
        null,
        true
    );

    if ( ! is_singular() ) {
        wp_enqueue_script( 'infinite_scroll' );
    }
}
add_action( 'wp_enqueue_scripts', 'custom_theme_js' );
2

wp_register_script() is for telling WordPress about script files, and is a PHP function. It has no control over how the script executes, only where the <script></script> tag is output and what the URL is.

If you don't want the script to execute until the page is loaded you need to put that JavaScript you posted in the relevant script file.

  • Okies, but the same function tells that Hey javascript load only when JQuery is loaded. – The WP Novice Jan 29 '18 at 10:56
  • 1
    No, it just tells it to place the tag for your script after jQuery's. It doesn't change anything about the script's execution at all. When loading scripts synchronously that's good enough. Similarly, if you set the $footer argument of wp_register_script() to true then you can have the script tag load in the footer, which is late enough for most scripts. – Jacob Peattie Jan 29 '18 at 11:10

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.