micah
micah

Reputation: 1975

which version of jQuery is running on a website with multiple libraries loading?

I am working on a project where there's multiple libraries loading in the head. The CMS being used is WordPress.

Through the wp_head, there's an enqueued script for the latest version 1.7.1, but just below it there's a set of script files that begins with version 1.4.

Here's a simple visual flowchart:

<head>    
  <script jQuery 1.7.1>
  <script jQuery 1.4>
  <script Colorbox>
  [7 more scripts dependent on 1.4 here]
</head>
...
<footer>
  <scripts that can use either 1.7.1 or 1.4>
</footer>

My understanding is that since jQuery 1.4 is below 1.7.1, that it is effectively the library being used.

Is it possible to rearrange the scripts so that those dependent on 1.4 can only be used by 1.4 then the rest by 1.7.1? For example:

<head>    
  <script jQuery 1.4>
  <script Colorbox>
  [7 more scripts dependent on 1.4 here]
  <script jQuery 1.7.1>
</head>
...
<footer>
  <scripts that can use either 1.7.1 or 1.4>
</footer>

See how I moved 1.7.1 down? Will 1.7.1's placement in the head below the 1.4 scripts affect the 1.4 scripts?

Is there a way to detect which version of jQuery is being used throughout the page?

Upvotes: 2

Views: 804

Answers (2)

wheresrhys
wheresrhys

Reputation: 23500

This should work as jQuery.noConflict(true) allows you to assign jQuery to any global variable and remove the original jQuery variable from the global namespace.

<head>    
  <script jQuery 1.4>
  <script type="text/javascript">
     var jq14 = jQuery.noConflict(true);
  </script>
  [7 more scripts dependent on 1.4 here] // do a find and replace in all these, replacing $ and jQuery by jq14
  <script jQuery 1.7.1>
</head>
...
<footer>
  <scripts that can use either 1.7.1 or 1.4>
</footer>

It's hardly best practice to load two jQuery's on the same page, but if your client won't pay to upgrade the above shoudl hopefully provide at least a workable compromise.

Upvotes: 0

Samuel
Samuel

Reputation: 540

please define those in "those dependent on 1.4", It is never a good idea to work back versions just for one part of a website. If it is multiple websites running the same header you might want to try and load in a php (databased) field and set the library that needs to be used in there. My advice would be that if indeed a part of your code doesn't work with the new library, update that code. And always use the latest JQuery :)

Upvotes: 2

Related Questions