2

I am creating a piece of JavaScript to be deployed to sites that are, at the moment, unknown to me.

This piece of code is supposed to do something that would be so much easier if I could just do it with jQuery, but I can't include any external libraries in this code because:

  1. I can't load other files, just the one JavaScript file that I write.
  2. Any external library might conflict with the unknown client site's code (they might already have loaded that same library into their site).
  3. I need the file to be downloaded as fast as possible.

Now my question is: Is there a tool that would help me extract just the specific code-paths my code uses from the external library (jQuery) so that I could embed them directly into my code as part of it (using namespaces, etc.)?

Or it could be that my question is even wrong to begin with.

1
  • I think thats what googs closure compiler:advanced mode does. But, code which execution branches based off runtime info makes static analysis really hard. Commented Jan 12, 2011 at 17:45

1 Answer 1

2

I know it conflicts with the first caveat but you could include jquery dynamically in your code by writing out the script element to a CDN e.g.

http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js

var headID = document.getElementsByTagName("head")[0];     
var script = document.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js';
script.onload = function() { initialiseJQuerySpecificCode(); };
headID.appendChild(script);

function initialiseJQuerySpecificCode() {
         jQuery.noConflict(); 
         //more jquery code
         jQuery(document.ready(function() { 
            //initialisetion code 
         }));
};

Loading from CDN means that many users will already have it in their cache. Also - the minified version is very small anyway.

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

3 Comments

Interesting, but this means that I am forced to wait for jQuery to load and also conflicts with the first item in my caveats...
The advantage of using the CDN is that many people will have jQuery cached already from other sites. So it will hopefully come from their browser cache - have a look at the StackOverflow source. Alternatively jQuery is open source so you could go and hack it to pieces if you liked .
Right. My question was if there were any way to automate that hacking-to-pieces.

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.