Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  131] [ 5]  / answers: 1 / hits: 93156  / 11 Years ago, sun, september 22, 2013, 12:00:00

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!


More From » php

 Answers
28

This blog post links to two plugins of interest:



Asynchronous Javascript

Improve page load performance by asynchronously loading javascript using head.js





WP Deferred Javascripts

Defer the loading of all javascripts added with wp_enqueue_scripts, using LABJS (an asynchronous javascript library).



Haven't tested them but checked the code and they do pretty fancy stuff with WordPress scripts enqueuing process.


But then WPSE comes to rescue:


// Adapted from https://gist.github.com/toscho/1584783
add_filter( 'clean_url', function( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) {
// not our file
return $url;
}
// Must be a ', not "!
return "$url' defer='defer";
}, 11, 1 );

[#75525] Friday, September 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;