How to not include properties in Javascript

200 views Asked by At

I'd like to dynamically include/remove a property at run time.

The problem I have is, using the Flotr2 Jquery library, I'm stuck in that the legend (a property) doesn't appear to be properly supported in IE. Due to time restraints, I've got the OK to do a quick hack.

So, this is a stripped version the code I have, including my attempt

Flotr.draw(
    container, myArrayForGraph, {
        if (!IsThisIe()) {
            legend: {
                show: true,
                container: legendContainer
            },
        }

    });

As you can see, I want to call 'IsThisIe()' and if it returns false, then add the legend property.

This isn't working, I'm assuming that we can't do this when Javascript expects a property. The message is

SyntaxError: missing : after property id

I know I could create 2 different objects and have something like

if (IsThisIe())
{
 Flotr.draw(
        container, myArrayForGraph, {
   ///etc
}
else
{
 Flotr.draw(
        container, myArrayForGraph, {
                legend:
//etc
}

But this is duplicating code, each object has many properties.

So, is there a way to not include properties in the manner I've described or am I stuck with having to manage the code in multiple places??

1

There are 1 answers

5
Niet the Dark Absol On BEST ANSWER

Try:

var params = {
    // define properties that should be set in both cases
};
if( !IsThisIe()) {
    params.legend = {show:true,container:legendContainer};
}
Flotr.draw(container, myArrayForGraph, params);

Then find out why IE doesn't like your code - start with the error console ;)


I have found the bug. It's a part of the code Flotr functions. Find where Flotr.DOM.node is defined, and replace it with this:

node:function(e) {var t = Flotr.DOM.create("div"),n; t.innerHTML = e; n = t.children[0]; return n;}

IE doesn't like it if you nuke the innerHTML of an element containing other elements that you are referring to. This fix prevents that issue.