javascriptdojointernet-explorer-8dom-eventsdojox.grid.datagrid

Switch case in Javascript is not working in IE8


Code snippet:

var role=s[0].Role;// role contains string value 
dijit.byId("editRole").attr("Value",getRoleByName(role));

function getRoleByName(role)
{
    var roleVal;
    alert(role);
    switch(role)
    {
        case 'Basic User' :roleVal='1';break;
        case 'Network Operator' :roleVal='3';break;
        case 'System Administrator' :roleVal='5';break;
        case 'Custom Level 1' :roleVal='11';break;
        case 'Custom Level 2' : roleVal='12';break;
        default: roleVal='1';break;
    }
    return roleVal;
}

When I tried to call the Javascript method which has the switch statement in it I am getting below error in IE8, but in Firefox is working fine..

Error in Developers tools:

method Error executing: function(/*Event*/ e){
    // summary:
    // Handler when the user activates the button portion.
    if(this._onClick(e) === false){ // returning nothing is same as true
        e.preventDefault(); // needed for checkbox
    } else if (this.type == "submit" && !this.focusNode.form){ // see if a nonform widget needs to be signalled
        for(var node=this.domNode; node.parentNode/*#5935*/; node=node.parentNode){
            var widget=dijit.byNode(node);
            if(widget && typeof widget._onSubmit == "function"){
                widget._onSubmit(e);
                break;
            }
        }
    }
}TypeError: Object doesn't support this property or method

Can anyone help me with this? ...how to overcome this problem?...


Solution

  • Error youre showing is not related to the below code. As the dojotoolkit-src commenting kindly shows you is, that it has to do with the function 'Handler when the user activates the button portion.'

    Should fix this

     // << Value is all lower case
     dijit.byId("editRole").attr("Value",getRoleByName(role)); 
    

    And a little cleaner coded switch with a 'WTF' - im thinking it might be the syntax highlighting youre experimenting with here - or are those tildes really in your code?

     // What are these back-tilds doing here?
     switch(role)``  
     {
          case 'Basic User'           : return 1
          case 'Network Operator'     : return 3
          case 'System Administrator' : return 5
          case 'Custom Level 1'       : return 11
          case 'Custom Level 2'       : return 12
          default:
               return 1
         // return works as your break does, it stops the switch
    }
    

    Saying your hitting problem during a trim? Try this, trim is not implemented in all javascript engines

    if(typeof String.prototype.trim !== 'function') {
            String.prototype.trim = function() {
                    return this.replace(/^\s+|\s+$/g, ''); 
            }
    }