Return to Snippet

Revision: 13742
at May 5, 2009 03:47 by amischol


Initial Code
jQuery.extend(jQuery, {
	checkFunction: function(fPointer, lastFpPointer){
		var newFunction = false;
		if(jQuery.isFunction(fPointer)){
			newFunction = fPointer;
		}else if (/\(.*\)/igm.test(fPointer)){
			newFunction = new Function(fPointer);
		}else{
			try{
				var mayBeFunction = (eval(fPointer));
				if(jQuery.isFunction(mayBeFunction)){
					newFunction = mayBeFunction;
				}else{
					newFunction = lastFpPointer;
				}
			}catch(e){
				newFunction =lastFpPointer;
			}
		}
		return newFunction;
	});

Initial URL

                                

Initial Description
Hi, all, this is the "checkFunction" extension for jQuery.

This extension takes two parameters:

   1.  fPointer
         This parameter is the "possible" function to check

         1.1   This parameter can be a String that is a function pointer   
                     ex:"editUser"

         1.2   This parameter can be a function
                     ex: function(idUser){alert('Edit User '+idUser);}

         1.3   This parameter can be a String that is a function body
                     ex: "alert('Edit User');"

   2.   lastFpPointer
           This parameter is the function by default if the fpPointer is not a valid function

           2.1   This parameter must be a function
                      ex: function(){}

This extension makes possibles to get any possible representation of a function and detect if it exist.

If it exist return it like a correct function.

If doesn't exist the default function will be returned.

The use of this extension:

Examples:


     -  jQuery.checkFunction("EditUser", function(){});

       returns EditUser
      


     -  jQuery.checkFunction("EditUser ();alert('Edit Launched');", function(){});

       returns  function(){EditUser ();alert('Edit Launched');}
      


     -  jQuery.checkFunction(EditUser, function(){});

       returns  EditUser
      
      

Thanks.

Initial Title
Check functions jQuery extension

Initial Tags
javascript, jquery, function

Initial Language
jQuery