title: Javascript - Passing function as string, then execute if it exists
body: <p>I have a javascript library which does returns a list of results to a div triggered on keyup events.  I want to use Jquery to apply a standard keyup event to all fields on all pages which have a certain class.
  That part I can do and it works OK.
  My issue is that the parameters which I use are dynamic and the last 2 of these are optional functions.</p>
  
  <pre><code>&lt;input type="text" 
  class="font8_input" name="wsFromPart" 
  value="&lt;%=wsFromPart%&gt;" id="wsFromPart" 
  size="20" maxlength="20" 
  onfocus="javascript:fncAjaxClear()" 
  onkeyup="javascript:fncAjaxSearch('wsDatabase','..\\AjaxBrowses\\PartBrowse.asp','wsFromPart','wsFromPartList','fncPrePartAjax',null);"/&gt;
  </code></pre>
  
  <p>At present, to control this I pass null if I don't have a function.</p>
  
  <p>I'm trying to get the to a position where I can define all of the fields like this.</p>
  
  <pre><code>&lt;input type="text" class="PartClass" name="wsFromPart" value="&lt;%=wsFromPart%&gt;" id="wsFromPart" /&gt;
  </code></pre>
  
  <p>Everything else will be added by setting classes/events by Jquery.</p>
  
  <p>I'm trying to work out how I can test if a function exists on my page, and only execute if it does.  I've tried passing the function name as a string but can't seem to make that work.  My last attempt is to have a generic function, and pass the name of the function which may exist to this function to evaluate and, if a function, execute it.</p>
  
  <pre><code>&lt;input type="text" class="font8_input" name="wsFrPt" id="wsFrPt" size="20" maxlength="20" value="&lt;%=wsFrPt%&gt;"  onfocus="javascript:fncAjaxClear()" onkeyup="javascript:fncAjaxSearch('wsDatabase','..\\AjaxBrowses\\PartBrowse.asp','wsFrPt','wsFrPtList',fncCheckFunction('fncPreAjaxPart1'),'fncPostAjaxPart');"/&gt;
  
  
  function fncAjaxSearch(wsDb,wsAsp,wsId,wsTarget,wsPreFunction,wsReturnFunction) {
  var myDate = new Date();
  var myDate1 = myDate.getTime();
  if (objXHR.readyState == 4 || objXHR.readyState == 0) {
      var wsDatabase = escape(document.getElementById(wsDb).value);
      var wsStr = escape(document.getElementById(wsId).value);
      var wsParam = "";
      if (wsPreFunction !== null) {
          wsParam = wsPreFunction();
      }
  //Only do ajax call if the 'source' field is not empty, otherwise empty target and clear border.
          if (document.getElementById(wsId).value &gt; '') {
          objXHR.open("GET", wsAsp + '?qryDatabase=' + wsDatabase + '&amp;qryDummy=' + myDate1 + '&amp;qrySearch=' + wsStr + wsParam, true);
          objXHR.onreadystatechange = function(){if(objXHR.readyState==4){fncAjaxSearchReturn(objXHR,wsId,wsTarget,wsReturnFunction)}};
          objXHR.send(null);
      }
      else {
          document.getElementById(wsTarget).innerHTML = '';
          document.getElementById(wsTarget).style.borderWidth = '0px';
      }
  }       
  </code></pre>
  
  <p>}</p>