jitScript - safe append javascript with registry handler. prevent duplicated


/ Published in: JavaScript
Save to your folder(s)

var lib = ['foo.js','foo.js','foo2.js','foo.js','foo333.js','foo2.js'];
var slib = 'fooooo.js';

lib.jitScript(); // append foo.js, foo2.js & foo33.js
slib.jitScript(); // append fooooo.js

enable firebug console
JitScriptDebug = true;

validate scripts
lib.isJsLoad();


Copy this code and paste it in your HTML
  1. var regJs = "";
  2. var JitScriptDebug = false;
  3.  
  4. var kaizekuJitScript = {
  5. src: function(){ kaizekuJitScript.jitScript(this);},
  6. jitScript: function(src){
  7. var iArray = (src instanceof Array) ? src : src.split(/s*,s*/) ;
  8. for (i = 0; m = iArray.length, i < m; i++){
  9. var uri = iArray[i];
  10. if (regJs.indexOf(uri) ==- 1) {
  11. var s = document.createElement('script');
  12. s.src = uri;
  13. if (s instanceof Object){
  14. try{
  15. document.body.appendChild(s);
  16. } catch(e){
  17. document.write('<script type="text/javascript" src="'+uri+'"><\/script>');
  18. }
  19. }
  20. regJs += uri + "|";
  21. } else {
  22. if (JitScriptDebug){
  23. kaizekuJitScript.fireBuglog("Reject duplicated entries: %s",uri);
  24. kaizekuJitScript.displayloadedJS();
  25. }
  26. }
  27. }
  28. },
  29. isJsLoad: function(){
  30. var iArray = (this instanceof Array) ? this : this.split(/s*,s*/) ;
  31. var d = regJs;
  32. for (i = 0; m = iArray.length, i < m; i++){
  33. var n = iArray[i];
  34. var r = (d.indexOf(n) ==- 1) ? false : true;
  35. if (JitScriptDebug) kaizekuJitScript.fireBuglog(n + " %s",r);
  36. return r;
  37. }
  38. },
  39. fireBuglog:function(str,ref){ if(typeof console != "undefine") { console.log(str,ref)} },
  40.  
  41. displayloadedJS:function(display){
  42. var ln = 'Loaded script\n', js = regJs.split("|");
  43. for(var i =0; i < (js.length - 1 ); i++){
  44. var str = '('+(i+1)+') : ' + js[i] + '\n'; ln += str;
  45. }
  46. ( (display == false ) ? alert(ln) : kaizekuJitScript.fireBuglog("%s",ln) );
  47. }
  48. };
  49.  
  50. Array.prototype.jitScript = kaizekuJitScript.src;
  51. String.prototype.jitScript = kaizekuJitScript.src;
  52. Array.prototype.isLoadJs = kaizekuJitScript.isJsLoad;
  53. String.prototype.isLoadJs = kaizekuJitScript.isJsLoad;

URL: http://blog.kaizeku.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.