This is a migrated thread and some comments may be shown as answers.

Javascript instanceof errors

3 Answers 118 Views
Input
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 22 Apr 2009, 06:24 PM
Hi, I have a configuration generated form that can contain controls of types RadTextBox, RadNumericTextBox, RadComboBox and RadDateInput. This form, generated at runtime, can contain 0 to X number of each control. I have a function (below) that I use to get all the controls of those types that match a certain ID naming scheme.

            function GetAllControls(filter)  
            { 
                var allLookupControls = [];  
                var allRadControls = $telerik.radControls;                 
                  
                for (var i = 0; i < allRadControls.length; i++)  
                {  
                    var element = allRadControls[i];  
                    if (element instanceof Telerik.Web.UI.RadNumericTextBox || element instanceof Telerik.Web.UI.RadTextBox || 
                        element instanceof Telerik.Web.UI.RadDateInput || element instanceof Telerik.Web.UI.RadComboBox)  
                    {  
                        if(element.get_id().match(filter))  
                        { 
                            Array.add(allLookupControls, element);  
                        } 
                    }  
                }  
                return allLookupControls;  
            } 

The function executes fine and returns the expected results as long as at least ONE control of each type exists on the page. If, for example, there are no RadDateInputs defined on the page however, I get a "Function Expected" javascript error. Using a javascript debugger I've been able to determine that the error occurs when trying to exeucte the "instanceof" function on the the DateInput type. Unfortunately, instanceof is the check I use to see if there are instance of the RadDateInput control so I can think of no way around this error.

Is there any help you can offer with this issue?

Thanks.

3 Answers, 1 is accepted

Sort by
0
ManniAT
Top achievements
Rank 2
answered on 23 Apr 2009, 11:52 PM
Hi,

this works for me:
function GetAllControls(filter) {  
    var allLookupControls = [];  
    var allRadControls;  
    try {  
        allRadControls = $telerik.radControls;  
    }  
    catch (x) {  
        return;  
    }  
 
    for (var i = 0; i < allRadControls.length; i++) {  
        var element = allRadControls[i];  
        if (TrySafe(element, Telerik.Web.UI.RadNumericTextBox) || TrySafe(element, Telerik.Web.UI.RadTextBox) ||  
                TrySafe(element, Telerik.Web.UI.RadDateInput) || TrySafe(element, Telerik.Web.UI.RadComboBox)) {  
            if (element.get_id().match(filter)) {  
                Array.add(allLookupControls, element);  
            }  
        }  
    }  
    return allLookupControls;  
}  
function TrySafe(element, kindOf) {  
    try {  
        if (element instanceof kindOf) {  
            return true;  
        }  
        return false;  
    }  
    catch (x) {  
        return false;  
    }  
}  
 
The first try-chatch is there for the possibility that NO (not a single one) Rad controls exist on the page.

HTH

Manfred
0
ManniAT
Top achievements
Rank 2
answered on 24 Apr 2009, 12:08 AM
Follow up:
This guide http://docstore.mik.ua/orelly/webprog/jscript/ch05_05.htm at 5.5.3 (last sentence) makes clear why the error occurs I guess :)
0
Sebastian
Telerik team
answered on 24 Apr 2009, 10:22 AM

Hello ManniAT,

Thank you for the comments and the code snippets you posted in this thread - they can be useful for other community members. I updated your Telerik points for the feedback.

Kind regards,

Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Input
Asked by
Andrew
Top achievements
Rank 1
Answers by
ManniAT
Top achievements
Rank 2
Sebastian
Telerik team
Share this question
or