1

So, I have the following js file which checks for few functions:

jQuery(document).ready(function() {
    RebindClickModal_show();
    RebindClickModal_contact();     
}); 

However if a page does not have these functions, then I get reference error.

So, I want to add a if condition to check to see if these functions exists first.

What would be the best way doing it?

Thanks.

3 Answers 3

2

try using isFunction like this:

jQuery(document).ready(function() {

    if ( $.isFunction($.fn.RebindClickModal_show) ) {
         RebindClickModal_show();
    }


    if ( $.isFunction($.fn.RebindClickModal_contact) ) {
        RebindClickModal_contact();  
    }

});
Sign up to request clarification or add additional context in comments.

Comments

1

You can check then wheather function exist using

jQuery(document).ready(function() {
    if(RebindClickModal_show && typeof RebindClickModal_show === typeof Function) {
        RebindClickModal_show();
    }

    if(RebindClickModal_contact && typeof RebindClickModal_contact === typeof Function) {
        RebindClickModal_contact();
    }
}); 

1 Comment

Thank you! Appreciate it! :)
1
jQuery(document).ready(function() {
    ($.isFunction(window.RebindClickModal_show))?( RebindClickModal_show() ):( "" );
    ($.isFunction(window.RebindClickModal_contact))?( RebindClickModal_contact() ):( "" );
});

function RebindClickModal_show(){
    console.log("Show - Hello World!");
}
function RebindClickModal_contact(){
    console.log("Contact - Hello World!");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.