6

I have built a few browser extensions that live in GMail. Since they are larger extensions, they incorporate jQuery 1.6.x.

I am using jQuery as a content script which means it is injected into GMail then my scripts reference jQuery as they are loaded afterwards.

I have found that when other extensions are installed alongside of my extensions and they incorporate earlier versions of jQuery the earlier versions are loaded first and my 1.6.x is ignored. The functionality I have that depends on 1.6.x no longer works, and that's a dealbreaker.

I'm trying to come up with an elegant solution for this. My first instinct is to namespace my version of jQuery, but loading jQuery twice seams clunky. Possibly testing for jQuery and then doing a diff, but that seems tedious.

Any thoughts?

Sara Chipps
  • 231
  • 1
  • 8

2 Answers2

2

You could possibly do what the HTML5Boilerplate does:

<script>window.jQuery || document.write('<script src="js/libs/jquery-1.5.1.min.js">\x3C/script>')</script>
alvincrespo
  • 616
  • 6
  • 7
2

My personal feeling is that you probably shouldn't be injecting a third-party library into the page anyway. Keep in mind, that -- since it's a Chrome extension -- you only need to support a single browser anyway.

So that means things like subscribing to events, Ajax, and all the other stuff that jQuery abstracts are all pretty easy because you don't need to worry about cross-browser compatibility.

You can even use document.querySelector to get much of the functionality of jQuery's selectors (not all, though).

As a bonus, it'll make your extension much more light-weight by not loading 100KB of javascript into every page...

Dean Harding
  • 19,871
  • 3
  • 51
  • 70