开发者

Partially Inlining External JavaScript Libraries

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜