2009
09.15

jqueryWhen I was working on recent posts, I was surprised to notice that jQuery script was loaded twice.

So I investigated it by looking at my current theme, as well as WordPress code. I found out that both WordPress and my current theme is loading jQuery.

My current theme is Pyrmont V2. I look at the header.php, I found out this:


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="<?php bloginfo('stylesheet_directory'); ?>/scripts/basic.js" type="text/javascript"></script>

So this is the reason why I found two jQuery source code being loaded (shown below).


But what about WordPress? In which file does the jQuery listed to be automatically loaded? After using Windows File-Content Search to scrutinize WordPress’wp-includes folder, I found the answer:

script-loader.php

   //...   
   $scripts->add( 'cropper', '/wp-includes/js/crop/cropper.js', array('scriptaculous-dragdrop'), '20070118');
   $scripts->add( 'jquery', '/wp-includes/js/jquery/jquery.js', false, '1.3.2');	
   $scripts->add( 'jquery-ui-core', '/wp-includes/js/jquery/ui.core.js', array('jquery'), '1.7.1' );	
   //...

I found it interesting that WordPress is loading jQuery from localhost (sodeve.net), but Pyrmont is loading it from Google. There must be a reason for this. Dave Ward at Encosia teaches us that the reasons are:

  1. Decreased Latency. Because Google is using Content Delivery Network (CDN). Therefore, it is much faster than your server
  2. Increased Parallelism. Because it reduces the number of connections made to a single server. Browser usually has limit on the number of connection to a single server
  3. Better Caching. Since the script is obtained from Google, it is very likely the script is already cached when visited any Google’s website that uses jQuery

Anyway, I’m sold to Dave’s idea. I replaced the line in script-loader.php, using http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js instead of /wp-includes/js/jquery/jquery.js. Then I observed the performance using Firebug.

Before disabling WordPress load script with ver parameter

Hmm.. WordPress added the “ver” parameter in the script request. This invalidated Dave’s point no. 3. What can we do to make WordPress not to add “ver” parameter in the script request?

How to Make WordPress Load Script without Ver Parameter

Open class.wp-script.php inside folder wp-includes of WordPress

//.....

$src = add_query_arg('ver', $ver, $src);

//.....

Comment the line shown above (line 117).

I believe things load faster now 😀 (At least for Google, sodeve.net server is a shared hosting, so its performance is difficult to expect :P)
After disabling WordPress load script with ver parameter

I hope it helps 😉 If you like this article, kindly consider to visit the sponsors to check out their offers. Cheers!

GD Star Rating
loading...
How to Prevent WordPress Loads jQuery Twice, 2.0 out of 5 based on 1 rating

Incoming Search Term

Advertise Here

2 comments so far

Add Your Comment
  1. Thanks for this, I was having the same problem with multiple jQuery’s loading and I’ve now removed them. Thanks.

  2. Will your edits be removed when you upgrade wordpress?