JavaScript error “Uncaught SyntaxError: missing) after argument list”

When we write JS, sometimes the error “Uncaught SyntaxError: missing ) after argument list” is reported, some friends are overwhelmed and do not know where the error is?

Literal translation means: syntax error: missing after parameter list )

Isn’t that what the missing parentheses mean? But is it only the missing brackets that cause this error? Not exactly. I was misled by this error today when I was doing a jQuery basic event exercise. I went back and forth three or four times, but there were no missing parentheses, right?

It wasn’t until I found the real culprit at …… that I realized that the error wasn’t just because there was a “real” parenthesis missing.

In fact, if you didn’t miss some parentheses, we should look for another reason, such as the following code.

$('.bannerList').append('<li><a target="_blank" href="' + bannerInfo.url + '" onclick="_gaq.push(['_trackEvent', 'app_ads_banner', 'banner', 'content_top',1,true]);"><img width="100%" alt="" src="' + bannerInfo.cover + '"></a></li>')

Let’s see where the problem is, after running will also report an error “Uncaught SyntaxError: missing ) after argument list”.

If you do not see, I will explain the reason, the reason for the error is the problem of splicing arguments in the onclick, direct writing is not possible, we need to escape the next, the following operation.

onclick="_gaq.push([\'_trackEvent\', \'app_ads_banner\', \'banner\', \'content_top\',1,true]);"

Of course, sometimes also related to single and double quotation marks, can not be used at the same time single and double quotation marks, each other can be included, this many children’s shoes will ignore the problem, here hint, remember to cackle later.

Well, I encountered these, if you have other methods, you can leave a message out, together with the next learning.

Leave a Reply