tags:

views:

18

answers:

1

Hi all, I am building a common function library but the functions inside need to reference different jquery files, which they may need to be referenced in some pages but not in others.

When I called this common function library in one web page which is only going to use one function, and I don't reference the files need it for the other function, then it will create a script error.

My question is if it would be possible to stop this script errors like...

//This if statement is what I was thinking to stop going through
    if ($(".objectdate") != null){
//This is the function that is calling other jquery files and creates error.
        $(document).ready(function() {
            $(".objectdate").datepicker({
        //Code inside.
        });
        }); 
    }

Thanks.

+1  A: 

If I understand correctly, you want to check if something like the datepicker exists before calling it, in that case you can check like this:

if($().datepicker != undefined) {
 $(document).ready(function() {
   $(".objectdate").datepicker({ 
   //Options...
   });
 });
}

You can use this approach to check almost any jQuery extension's presence, just check that the $().whatever is defined.

If you want to check if jQuery exists at all, check like this:

if(window.jQuery) { 
 //or window.$ for your case specifically, in case of jQuery.noConflict()
}
Nick Craver
Thanks for the answer, just what I need it. :-) +1.
Cesar Lopez