Pssst... Listen to some of my music while you debug your script in IE!
JavaScript Error in IE
By Scott HorlbeckError: Expected identifier, string or number
IE strikes again. I was working on the following code in Firefox without any problems.
myFunction({
abstract_link : {
type : 'text', size : '75', class : 'edit_article', hint : 'blah blah'
},
authornames : {
type : 'text', size : '75', class : 'edit_article', hint : getNotice('author')
},
institutionname : {
type : 'text', size : '75', class : 'edit_article', hint : getNotice('institution')
},
sourcename : {
type : 'text', size : '75', class : 'edit_article', hint : getNotice('source')
}
});
The code is designed to spit out html for a bunch of <input> elements with each key pair giving us the attributes. For example:
<input type="text" size="75" class="edit_article" /> <!-- "hint" is handled with special attention. -->
I decided to test it in IE. Much to my chagrin, I received this error: Expected identifier, string or number. Google helped me discover that, for most people, the problem is a misplaced/additional comma. However, I could not find ANY erroneous commas.
Long story short: as the syntax highlighting above shows, the word "class" is a reserved word in ECMA Script. The solution is to enclose it in quotes, as below:
myFunction({
abstract_link : {
type : 'text', size : '75', "class" : 'edit_article', hint : 'blah blah'
},
authornames : {
type : 'text', size : '75', "class" : 'edit_article', hint : getNotice('author')
},
institutionname : {
type : 'text', size : '75', "class" : 'edit_article', hint : getNotice('institution')
},
sourcename : {
type : 'text', size : '75', "class" : 'edit_article', hint : getNotice('source')
}
});
If you wish to make your code more consistent, you can enclose all your keys in quotes, though it is not required.