Reputation: 1010
How to check if is function on jquery, but function is in another .js file?
validation.js:
if ($.isFunction('payment')) {
$('[data-numeric]').payment('restrictNumeric');
$('.cc-number').payment('formatCardNumber');
$('.cc-exp').payment('formatCardExpiry');
$('.cc-cvc').payment('formatCardCVC');
}
this is false because func payments is in the payments.js .
Upvotes: 20
Views: 56526
Reputation: 1010
problem is solved. its works:
if ($.fn.payment) {
//do something
}
Upvotes: 23
Reputation: 7593
You can check if a function exists using window
For example
var fn = window['NameOfTheFunction'];
if(typeof fn === 'function') {
doSomething();
}
If your function in payment.js is part of a self contained function, you need to set it to so the window object can "see" it by adding this in your self contained function:
window.NameOfTheFunction = NameOfTheFunction;
Upvotes: 6
Reputation: 7768
Try to check like as follows,
if (typeof payment !== 'undefined' && $.isFunction(payment)) {
$('[data-numeric]').payment('restrictNumeric');
$('.cc-number').payment('formatCardNumber');
$('.cc-exp').payment('formatCardExpiry');
$('.cc-cvc').payment('formatCardCVC');
}
Upvotes: 9
Reputation: 25352
Try like this
if (typeof payment === "function")
{
// Do something
}
Upvotes: 40