0

I have the following array in a JS file:

   var arrayPriority = [
      {
         classPrio : "hoverLineTable",
         color : "#99bfe6"
      },
      {
         classPrio : "selectionLigneTable",
         color : "#FEDA84"
      },
      {
         classPrio : "jqGrCorrespondance",
         color : "#CAFDA8"
      }
   ];

It works well except in IE where I get this error on the line classPrio : "hoverLineTable":

Identicateur, string or number expected

What is the issue and how can I fix it?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Kvasir
  • 1,197
  • 4
  • 17
  • 31
  • 1
    This is a JavaScript array, not JSON. I don't see anything wrong with it. – Felix Kling Jun 18 '15 at 07:51
  • Thank for the information ;) like it's look like json i thinked it was json ;) – Kvasir Jun 18 '15 at 07:52
  • The quoted code is just fine, and IE is perfectly happy with it: http://jsfiddle.net/8kwbchrq/ – T.J. Crowder Jun 18 '15 at 07:53
  • Json and Javascript are basically the same thing, json is simply a string representation of a javascript oject – Liam Jun 18 '15 at 07:53
  • No errors found in that piece of code – kosmos Jun 18 '15 at 07:53
  • 1
    @Kvasir: If it were JSON, A) It wouldn't be in JavaScript source code (unless it was inside a string), and B) The property names would be in double quotes. – T.J. Crowder Jun 18 '15 at 07:53
  • 1
    @Liam: No, JSON is a data notation, JavaScript is a programming language. JSON is a **subset** of JavaScript's object initializer syntax. – T.J. Crowder Jun 18 '15 at 07:54
  • Try to put your classPrio and color between quotes ". It should work. – Alex Jun 18 '15 at 07:54
  • 1
    I would say that the error comes from another line. Check commas. –  Jun 18 '15 at 07:54
  • @Alex: There's no need for that in JavaScript. – T.J. Crowder Jun 18 '15 at 07:54
  • 2
    @Liam: JSON is data format, JavaScript is a programming language. They are hardly the same thing. Same parts might *look* similar, but that doesn't make it the same. Duck typing does not apply here. – Felix Kling Jun 18 '15 at 07:54
  • My point, is telling the OP this isn't really helping @FelixKling the symantics of this isn't his problem – Liam Jun 18 '15 at 07:55
  • 1
    Stop discussing what JSON is, what the difference is and why and so.. concentrate on the answer, this is not the default forum where the question about "hi" is ending with the answer about life. So that my comment is not fully senseless: Which IE do you use? – Cagatay Ulubay Jun 18 '15 at 07:56
  • You should then review your previously written code, as others stated. Maybe opened quotes or missing ;. – Alex Jun 18 '15 at 07:56
  • 1
    @Liam: That's why it's a comment, not an answer. But I disagree, educating someone about correct terminology will help them to properly describe their problems later on. – Felix Kling Jun 18 '15 at 07:58
  • I use IETester for simulate an IE and no matter the version i use i still have the error but when i try on my Internet Explorer 11 i don't have the error – Kvasir Jun 18 '15 at 08:03
  • Put a couple of `alert(N);` (N = 1, 2, 3, ...) into your code in order to locate the problem precisely. –  Jun 18 '15 at 08:08

2 Answers2

-1

I apologize i find the problem it come from the fact i don't modify the good file so i let the good file like that :

 var arrayPriority = [
      {
         class : "hoverLineTable",
         color : "#99bfe6"
      },
      {
         class : "selectionLigneTable",
         color : "#FEDA84"
      },
      {
         class : "jqGrCorrespondance",
         color : "#CAFDA8"
      }
   ];

And it's look like old version of internet explorer don't like when we use the keyword : class. So if i put 'classPrio' it's work.

Thanks for your help

Kvasir
  • 1,197
  • 4
  • 17
  • 31
  • Indeed... `class` is a reserved keyword :-) –  Jun 18 '15 at 08:31
  • Yes but it's work on chrome and firefox and also on IE 11 but not on the other verison but yea now i understand not use this one ;) – Kvasir Jun 18 '15 at 08:34
  • There are many differences between browsers, this is the reason of being of this "cross browser testing" stuff :-D –  Jun 18 '15 at 08:36
  • 1
    @wawawared & Kvasir: In ES3, reserved words couldn't be used as literal property names (in object initializers, or `foo.propName`, etc.), which is why the DOM has `className` instead of `class` and `htmlFor` instead of `for` on DOM elements. That changed with ES5 (~six years ago), which allows them there, since they aren't actually ambiguous (there's no chance that the `for` in `someElement.for` is meant to start a `for` loop). Old browsers like IE8 that predate ES5 apply the old rules. Modern browsers apply the current rules. – T.J. Crowder Jun 18 '15 at 09:50
  • @Kvasir you should have posted the right array. :( – Swaprks Jun 18 '15 at 12:30
-2

You just need to wrap the keys in double inverted commas.

var arrayPriority = [
      {
         "classPrio" : "hoverLineTable",
         "color" : "#99bfe6"
      },
      {
         "classPrio" : "selectionLigneTable",
         "color" : "#FEDA84"
      },
      {
         "classPrio" : "jqGrCorrespondance",
         "color" : "#CAFDA8"
      }
   ];
Swaprks
  • 1,553
  • 11
  • 16
  • 2
    No, you don't. JS property names can be strings or identifiers. Leaving the quotes off (for those names at least) is fine. – Quentin Jun 18 '15 at 09:01
  • 1
    @Quentin: And yet, amusingly, if the OP had actually posted [their real code trying to use `class: "..."`](http://stackoverflow.com/a/30910245/157247), this answer would have been right (for old browsers like IE8). – T.J. Crowder Jun 18 '15 at 09:51
  • @Quentin Yes..I assumed that he was using some sort of attribute. It was considered as a class which is a keyword and it needs to be inside quotes. – Swaprks Jun 18 '15 at 11:52
  • @T.J Crowder Negative ratings even though i was right.. :( – Swaprks Jun 18 '15 at 11:53
  • @Swaprks: Well, it was wrong at the time you posted it. :-) But yes, it's right (for old browsers) now the OP has changed things. – T.J. Crowder Jun 18 '15 at 11:57