admin管理员组

文章数量:1356084

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.

<input type="text" 
class="font8_input" name="wsFromPart" 
value="<%=wsFromPart%>" id="wsFromPart" 
size="20" maxlength="20" 
onfocus="javascript:fncAjaxClear()" 
onkeyup="javascript:fncAjaxSearch('wsDatabase','..\\AjaxBrowses\\PartBrowse.asp','wsFromPart','wsFromPartList','fncPrePartAjax',null);"/>

At present, to control this I pass null if I don't have a function.

I'm trying to get the to a position where I can define all of the fields like this.

<input type="text" class="PartClass" name="wsFromPart" value="<%=wsFromPart%>" id="wsFromPart" />

Everything else will be added by setting classes/events by Jquery.

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.

<input type="text" class="font8_input" name="wsFrPt" id="wsFrPt" size="20" maxlength="20" value="<%=wsFrPt%>"  onfocus="javascript:fncAjaxClear()" onkeyup="javascript:fncAjaxSearch('wsDatabase','..\\AjaxBrowses\\PartBrowse.asp','wsFrPt','wsFrPtList',fncCheckFunction('fncPreAjaxPart1'),'fncPostAjaxPart');"/>


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 > '') {
        objXHR.open("GET", wsAsp + '?qryDatabase=' + wsDatabase + '&qryDummy=' + myDate1 + '&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';
    }
}       

}

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.

<input type="text" 
class="font8_input" name="wsFromPart" 
value="<%=wsFromPart%>" id="wsFromPart" 
size="20" maxlength="20" 
onfocus="javascript:fncAjaxClear()" 
onkeyup="javascript:fncAjaxSearch('wsDatabase','..\\AjaxBrowses\\PartBrowse.asp','wsFromPart','wsFromPartList','fncPrePartAjax',null);"/>

At present, to control this I pass null if I don't have a function.

I'm trying to get the to a position where I can define all of the fields like this.

<input type="text" class="PartClass" name="wsFromPart" value="<%=wsFromPart%>" id="wsFromPart" />

Everything else will be added by setting classes/events by Jquery.

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.

<input type="text" class="font8_input" name="wsFrPt" id="wsFrPt" size="20" maxlength="20" value="<%=wsFrPt%>"  onfocus="javascript:fncAjaxClear()" onkeyup="javascript:fncAjaxSearch('wsDatabase','..\\AjaxBrowses\\PartBrowse.asp','wsFrPt','wsFrPtList',fncCheckFunction('fncPreAjaxPart1'),'fncPostAjaxPart');"/>


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 > '') {
        objXHR.open("GET", wsAsp + '?qryDatabase=' + wsDatabase + '&qryDummy=' + myDate1 + '&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';
    }
}       

}

Share Improve this question asked Jan 8, 2012 at 15:50 KeithKeith 8014 gold badges12 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

If it is a global, you can do this

var fnc = window["yourFunctionName"];  //Use bracket notation to get a reference
if( fnc && typeof fnc === "function" ) {  //make sure it exists and it is a function
    fnc();  //execute it
}

If it is namespaced, you can do the same type of thing, just involves some looping.

var myFnc = "foo.bar.fun";
var nameParts = myFnc.split(".");  //split up the string into the different levels
var fnc = window;  //set it to window
for(var i=0;i<nameParts.length;i++){  //loop through each level of the namespace
    fnc = fnc[nameParts[i]];
    if(!fnc){  //make sure it exists, if not exit loop
        fnc = null;
        break;
    }
}
if( fnc && typeof fnc === "function" ) {  //make sure it exists and it is a function
    fnc();  //execute it
}

You can check if function exists by:

if(typeof(yourFunctionName) == "function")
//...do your code
else
//...function not exists

if you passing the function name as a string, then change typeof(yourFunctionName) to typeof(eval(yourFunctionName))

本文标签: JavascriptPassing function as stringthen execute if it existsStack Overflow