It is better to use the function without preliminary scanning dom to create a cache, preliminary scanning is not required when you use a simple function with building a cache. Using jQuery you can create such a function (your own javascript method below):
window.__jcache = {}; window.$jc = function(u) // jQuery cache { if( u == undefined ) { return window.jQuery; } if( typeof u == 'object' || typeof u == 'function' ) { return window.jQuery(u); } if( window.__jcache[u] == undefined ) { window.__jcache[u] = window.jQuery(u); } return window.__jcache[u]; };
Or no frame (native javascript):
window.__domcache = {}; window.getObj = function(u) // jQuery cache { if( u == undefined ) { return null; } if( typeof u == 'object' || typeof u == 'function' ) { return u; } if( window.__domcache[u] == undefined ) { window.__domcache[u] = document.getElementById(u); } return window.__domcache[u]; };
So you can do:
var o = $jc('a1'); // for jquery version var o = getObj('a1'); // The native javascript method (jamex)
This is a trick.
Greetz, Erwin Haantjes
source share