Is there any way to load scripts in WordPress asynchronously?

I looked through the WordPress documentation and couldn’t find any mention of async javascript loading using wp_enqueue_script(), is it possible at all? I have a couple of scripts and would rather async load them to improve the user loading experience!

Related posts

Leave a Reply

1 comment

  1. You can use wp_enqueue_script() to load a “script loader”, which is a script that loads other scripts. That way, you are loading the bootloader synchronously, but the others asynchronously.

    Your script-loader could contain this:

    var scriptUrls = [URLS_HERE];
    
    scriptUrls.forEach(function(url,index,scriptUrls){
      var script = document.createElement('script');
      document.getElementsByTagName('head')[0].appendChild(script);
      script.src = url;
    });