Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I defer or async this WordPress javascript snippet to load lastly for faster page load times?

I have various javascripts that are necessary plugins in one of my WordPress domains, and I know where in the php file it's called from.

I'm taking every measure I can take to speed up page loading times, and every speed tester on the web says to defer javascripts if possible.

I have read about the defer='defer' and the async functions in javascript and I think one of these will accomplish what I'm trying to accomplish. But I'm not understanding how I'd do so in a php file.

For instance, here is a snippet from one particular plugin's php file where the javascript file is being called:

function add_dcsnt_scripts() {      wp_enqueue_script( 'jquery' );     wp_enqueue_script( 'dcsnt', dc_jqsocialtabs::get_plugin_directory() . '/js/jquery.social.media.tabs.1.7.1.min.js' );  } 

I've read that it's best to do something like this for faster page loading times:

<script defer async src="..."></script> 

But I don't know how to accomplish that within a php file. I want to do this with all of my javascript files.

How would I accomplish deferring or asyncing this javascript snippet to is loads last and speeds up page load times? What would be the ideal way to increase page load times across all browsers? Thanks for any guidance anybody can offer!

like image 836
Jason Weber Avatar asked Sep 22 '13 12:09

Jason Weber


People also ask

Is async faster than defer?

Whereas DEFER scripts don't execute until the HTML document is done being parsed (AKA, DOM Interactive or performance. timing. domInteractive). Comparing the ASYNC and DEFER waterfalls, we see that using DEFER makes DOM Interactive fire sooner and allows rendering to proceed more quickly.

What do we achieve by deferring the loading of JavaScript?

Overview. Deferring loading of JavaScript functions that are not called at startup reduces the initial download size, allowing other resources to be downloaded in parallel, and speeding up execution and rendering time.

How do I remove the defer from the script tag in WordPress?

To remove that “defer”, you have to find how it is loaded in the first place (e.g. could be hardcoded in your theme or child theme) and alter the source code, making sure you won't break the functionality. Let me know if I can further assist you at your earliest convenience!


1 Answers

Or more universal way:

function add_async_forscript($url) {     if (strpos($url, '#asyncload')===false)         return $url;     else if (is_admin())         return str_replace('#asyncload', '', $url);     else         return str_replace('#asyncload', '', $url)."' async='async";  } add_filter('clean_url', 'add_async_forscript', 11, 1); 

so you can add async to any script without code changes, just add #asyncload to script url as:

wp_enqueue_script('dcsnt', '/js/jquery.social.media.tabs.1.7.1.min.js#asyncload' ) 
like image 134
bestlion Avatar answered Oct 01 '22 06:10

bestlion