2

I need to rotate a DIV, but that is not the problem I am facing, it is JSON parsing, I want to be able to get the proper attr value from the variable rotateAttrSet but I want to get it according to the browser type

I can do var rotateAttr = rSET.FF;, but I can't do var rotateAttr = rSET.brwShort;

Is there a way to make this work? again, I am not looking for ways to rotate the DIV, I just want to know if there is a way to get the JSON work by a variable (like .brwShort below)

Thanks

<script>
var rotateAttrSet = '{"IE":"-ms-transform","FF":"-moz-transform","CR":"-webkit-transform","SF":"-webkit-transform","OP":"-o-transform","WC3":"transform"}';

function rotator(o)
{
    var o = $(o);
    var angle = 0;

    var rSET = parseJSON(rotateAttrSet);
    var brwShort = "FF";//getBrowser().split(";")[2];
    var rotateAttr = rSET.brwShort;

//alert(rotateAttr);
    //o.removeAttr("onClick");
    setInterval(function(){
        angle++;
        if(angle == 360) angle = 0;
        o.text(angle);
        o.css(rotateAttr, "rotate("+angle+"deg)");
    }, 10);
}

function parseJSON(s)
{
    return eval('('+s+')');
}

</script>
  • 1
    If you're using jQuery, you should be calling `$.parseJSON`, not your custom function. – bfavaretto Aug 29 '13 at 14:31
  • 1
    Is this your actual code? If so, why do you need to even need to parse it as JSON? Just remove the quotes from each end and it's a perfectly normal javascript object, and you don't need to do the parsing at all. Also, don't use `eval()` to parse JSON. All browsers IE8 or newer include the `JSON.parse()` function. For IE7 or other old browsers, you should use the third party json2.js script. – Spudley Aug 29 '13 at 14:34

1 Answers1

3

You need to use the browser short as a key as follows:

var brwShort = "FF";//getBrowser().split(";")[2];
var rotateAttr = rSET[brwShort];

Otherwise, it is actually looking for a property on the object with a key of "brwShort", which doesn't exist on your object.

AgentD5
  • 148
  • 13
  • Yes, `obj.property` is synonymous with `obj['property']` (assuming `var obj = {'property':'foo'}` both will give you `foo`) – Brad Christie Aug 29 '13 at 14:32