How to Check If jQuery Plugin is Loaded
A few days ago I was playing with jQuery plugin jTemplates. I had a tough time trying to get it working. As usual in such situation you can’t see obvious mistakes and instead you keep exploring all options which can possibly go wrong. During one of such exploration I was checking if jTemplates has been correctly loaded. Of course real problem was completely different… :)
But still, the code checking if plugin is loaded is very simple and thanks to it you have one less option to worry about. Here it is:
//check if jTemplates is loaded
if (!jQuery.fn.processTemplate) {
alert("jTemplates is not loaded");
return;
}
jTemplate has a function called processTemplate. If entire plugin was loaded correctly then this function is available. And this is exactly what is checked within if() statement. If for some reason plugin wasn’t loaded user will get an information: “jTemplates in not loaded”.
Of course you should use more user-friendly messages or deal with such problems in more elegant way. But nevertheless, checking if plugins are loaded should be a mandatory step for all developers. It doesn’t cost you much but in rare (hopefully) situations your site won’t crash because of lack of plugin which you expect to be loaded.
Thank you very much …… great keep going
Saeed7
15 Feb 12 at 7:59 am