06 March, 2014

How To Check Javascript Fuction Available OR Not

Problem:
Some time we need to call javascript function which is based on some condition or may pass as parameter and then call that function. In this case we need to check whether this javascript function available or not if available then we call it otherwise not.

 HTML:
<input id="test" value="Available" onclick="ISfuctionAvailable();" type="button"/>
 
<input id="test1" value="Not available" onclick="fuctionNotAvailable();" type="button"/>

Javascript:
function ISfuctionAvailable() {
    if (typeof fuctionNotAvailable == 'function') {
        alert('Function available');
        //You can call function like  fuctionNotAvailable();
    }
    else {
        alert('Function Not available');
    }
}
 
function fuctionNotAvailable() {
    if (typeof test5 == 'function') {
        alert('Function available');
    }
    else {
        alert('Function Not available');
    }
}

Demo:
http://jsfiddle.net/patelriki13/drwdG/




















1 comment: