Solved: Knockout binding error “Expected identifier, string or number” on IE 8

Problem:

You use Knockout 3.0 in your web pages, and it works fine with all browsers but IE 8. On IE 8, it throws the error:

Expected identifier, string or number

which is useless for debugging.

Expected identifier, string or number

Analysis:

Check your binding expression in the HTML tags, are you using any javascript keywords? For example, the following expressions caused the error on IE 8 in my project:

<label data-bind="attr: { for: 'course-type-selector' }">Course Type: </label>

There is a javascript keyword “for” inside the data-bind expression which caused the problem on IE 8.

Solution:

Wrap around the javascript keyword with quotation marks. For example:

<label data-bind="attr: { ‘for’: 'course-type-selector' }">Course Type: </label>

Remark:

Always quote the javascript keywords in your projects to avoid problems on IE 8. This is very important! Not only with Knockout js, but also with all other javascript code. For example, if you use an object has a field called class, then on IE 8, the code won’t execute and won’t give you any error information, it just doesn’t work and keep in silence.

{
	name: "test",
	class: "fancy"
}

You can’t troubleshoot this issue with normal debug, and can only figure out the root cause by experience. The above code should be written like this:

{
	name: "test",
	"class": "fancy"
}

So again, always quote the javascript keywords your projects to avoid problems on IE 8.

Add comment

Loading