2

I have a project that requires a custom JavaScript library to be included in end user's websites. Sort of a third party thing, think JavaScript tracking like Google Analytics.

We'll have no control over what other JS libraries/frameworks might also be loaded or what versions.

I'd like to be able to leverage jQuery's event delegation, selector and AJAX functionalities but:

  • Not cause any conflicts with other libraries or other versions of jQuery that might be loaded
  • Not require the end user to have to think about including jQuery separately.

So, rolling in all of jQuery sort of seems like overkill but again, event delegation, selector and AJAX are required. I know jQuery's sizzel engine is broken out in such a way that it's possible to include it in 3rd party libraries and there are plenty of tiny AJAX libraries but we need good event delegation support as well. Additionally, I foresee us needing to do some heavy DOM lifting with this library in the near future as well so it's arguable we do need most of jQuery's functionality.

Any suggestions on how to encapsulate jQuery in such a way that we don't trample over anyone's code? Also, how advisable is this? It does feel a tad iffy.

Also, what's the best wat to encapsulate it into another library? Is there a better way than this?:

(function(window){
    window.myNamespace = {
        _jq:null,
        init: function(){
            // Include jQuery
            myNamespace.setJq();
        },
        setJq:function(){
            /*! jQuery v1.8.2 jquery.com | jquery.org/license */
            (function(a,b){function G(a){...}}) // <- minified jQuery
            // Stash a local copy of jQuery
            myNamespace._jq = jQuery;
            // Return $ and jQuery namespace
            $.noConflict(true);
        }
    }
})(window)

(We intend on offering a version of the library without jQuery for those savvy enough to know that it's already loaded on their page and what version they're using)

3 Answers 3

2

Have a look at jQuery in Parts: https://github.com/mythz/jquip

Sign up to request clarification or add additional context in comments.

Comments

1

What about dong a conditional load... Test if $ exists. If it does, don't load the script. If it doesn't, then load it.

4 Comments

Doesn't solve the problem if they are using Prototype or jQuery 1.3
Actually, what I did was test to see if jQuery has been loaded and what version. If the correct version is loaded, I just pass a reference for jQuery. Otherwise, I dynamically load jQuery, with a callback which returns the jQuery namespace and $ back and stores a noConflict version locally. --- I marked Petah's response as the answer because it should have worked for me (and probably would for most) but the library size (after I include all the jQuery features I need) was basically the same.
Nice. :) glad you found a way that worked. Why don't you write your own answer and mark it as the correct answer for people in the future?
Because wrapping jquip in your own library was probably an appropriate answer for the question title. --- My need changed as I realized I didn't want to penalize users by shipping an unnecessarily large JS asset if they already had jQuery loaded or cached (when I add it dynamically, I pull from Google's CDN). I'll probably append my solution to the question and point to this answer/comment chain for others in the future.
0

You could use the following pattern to make sure that the jQuery is being passed in to the function and then you can map it to the local variable $ without issues.

(function(window,$){ //use $ as a local jQuery variable

})(window,jQuery);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.