Conditionally including javascript and css files
how to include/inject css or javascript on demand from within body of the docuement
To include the javascript and css resources you can use the following two methods.
function injectJS (fileName, jsbase){
var src = jsbase + '/' + fileName + '.js';
var ipts = document.getElementsByTagName("script");
var found = false;
for (i=0; i < ipts.length; i++){
var type = ipts[i].src;
if (type.indexOf(fileName) != -1){
found = true;
}
}
if (!found){
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = src;
headID.appendChild(newScript);
}
}
function injectCSS (fileName, cssbase){
var ipts = document.getElementsByTagName("link");
var found = false;
for (i=0; i < ipts.length; i++){
if ( ipts[i].rel != 'stylesheet') continue;
var type = ipts[i].href;
if (type.indexOf(fileName) != -1){
found = true;
}
}
if (!found){
var headID = document.getElementsByTagName("head")[0];
var c = document.createElement('link');
c.type = 'text/css';
c.rel = 'stylesheet';
c.href = cssbase + '/' + fileName + '.css';
headID.appendChild(c);
}
}
Note that the fileName should not contain the .js or .css extension.
The methods can be called from anywhere in the body of the html page (from inside standard script tags/block).
Above methods make sure that the resource being injected is not included more than once. Feel free to use the code and modify it to suit your heart's content. No strings attached!
Required to claim my blog!
I had no option but to write this post so that I cam claim my Blog! At least technorati says so.. Technorati Profile
NetBeans 6 Preview is quite stable.
Today, I downloaded and tested NetBeans 6 Preview release (M9). It seems to be stable enough for production work as well. We were waiting for some stable release of Net Beans IDE version 6 for quite some time. This was since we had decided to use unified EL in new version of our web application framework 'netForce'. Tomcat 5.x does not support unified EL; and NetBeans 5.x does not support Tomcat 6. So we had no option but to wait for the release of version of NetBeans IDE that can support Tomcat 6. NetBeans 6 is scheduled to be released in last quarter of 2007 but the preview release seems to be stable and promising enough to encourage us to use this IDE for active development of new version of our applications. Kudos to the NetBeans team. We are also impressed with other overall improvements in IDE. Most of our work is done using NetBeans IDE. The only exception being reporting, for which we have to use Eclipse since we are using BIRT reporting engine. BIRT being an open source software, I wonder if someday we will have a NetBeans module providing support for BIRT.
Falling in love with jQuery
So if you are looking for a great javascript library, go for jQuery. It's at jQuery.com
For me, I confess I have fallen i love with jQuery.