IE versions earlier than 9 cause the error "Expected Identifier, String or Number"

This expression for binding knockout 2.1 works fine in Firefox and IE9, but it fails in IE9 compatibility mode with the error "Expected identifier, string or number":

<div data-bind="template: { if: myDataModel, data: myDataModel, afterRender: setup(myDataModel) }"> 

I found the actual place in the debugger, this is a line of code (knockout-2.1.0.debug.js):

 return new Function("sc", functionBody) 

functionBody is the string equal to the expression above. I tried to play with spaces and carriage returns - nothing helps, the same results: it works as expected with any browser other than IE9 compatibility mode

Any suggestions?

+6
source share
1 answer

I think the problem is that older versions of IE do not like ifs or similar reserved words as property names. Try adding single quotes around property names.

 <div data-bind="template: { 'if': myDataModel, data: myDataModel, afterRender: setup(myDataModel) }"> 

Another common time you will get is when you have a class binding. Same fix:

 <tr data-bind="attr: { 'class': packageSelected() ? 'success' : '' }"> 

List of reserved words in JS: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Reserved_Words

+12
source

All Articles