最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught SyntaxError: Unexpected token var - Stack Overflow

programmeradmin1浏览0评论

I have an error Uncaught SyntaxError: Unexpected token var displayed between (1) and (2) its a very odd error and it doesn't make sense at all.

if ($hiddenimage.length==0) { //if this is the first time moving over or clicking on the anchor link
    var $hiddenimage=$('<img src="'+this.href+'" />').appendTo($hiddenimagediv) //populate hidden div with enlarged image
    $hiddenimage.bind('loadevt', function(e){ //when enlarged image has fully loaded
        loadarea.empty().append($.thumbnailviewer2.buildimage($, $anchor, s, options)
        (1) - var $targetimage=$.thumbnailviewer2.buildimage($, $anchor, s, options) //create reference actual enlarged image
        (2) - $loadarea.empty().append($targetimage) //show enlarged image
        $.thumbnailviewer2.showimage($targetimage, s)
    })

I have an error Uncaught SyntaxError: Unexpected token var displayed between (1) and (2) its a very odd error and it doesn't make sense at all.

if ($hiddenimage.length==0) { //if this is the first time moving over or clicking on the anchor link
    var $hiddenimage=$('<img src="'+this.href+'" />').appendTo($hiddenimagediv) //populate hidden div with enlarged image
    $hiddenimage.bind('loadevt', function(e){ //when enlarged image has fully loaded
        loadarea.empty().append($.thumbnailviewer2.buildimage($, $anchor, s, options)
        (1) - var $targetimage=$.thumbnailviewer2.buildimage($, $anchor, s, options) //create reference actual enlarged image
        (2) - $loadarea.empty().append($targetimage) //show enlarged image
        $.thumbnailviewer2.showimage($targetimage, s)
    })
Share Improve this question edited Dec 30, 2010 at 8:00 cdhowie 169k25 gold badges299 silver badges319 bronze badges asked Dec 30, 2010 at 7:51 Bob RBob R 6171 gold badge13 silver badges27 bronze badges 1
  • 2 The error is due to an unclosed parentheses . – Black Mamba Commented Apr 10, 2017 at 9:24
Add a comment  | 

4 Answers 4

Reset to default 22

Count the open parentheses on this line:

loadarea.empty( ).append($.thumbnailviewer2.buildimage($, $anchor, s, options)
              ^ ^       ^                             ^                      ^
              1 0       1                             2                      1

Add another closing paren; the parser thinks you're still specifying arguments to the append() function, and the var keyword is invalid in this context.

Also, use semicolons. If not for your sake, do it for Douglas' health.

I had a similar error message in the console with the minifier parsing my javascript source code. I found that using // comments like so always interrupted the minification process, and gave me an error in the console. Therefore i switched all /* comments */ like so. MDN Javascript Comments And immediately everything parsed as expected. Hope it helps.

I got the same error because I have added ','(comma) instead of ';'(semi-colon). IF you get this error, please re-check your code carefully.

For me, the problem was spacing when declaring variables. I had a javascript script that was getting loaded from our db dynamically on the page. I had copied the script and create a new entry for sql database. While, I copied the script, the copy action actually ignored the newlines (\n) or the select statement gave everything merged in a single line ignoring the newlines(\n). My javascript looked like this:

window.onload = function(){ var abc = 'xyz';}var xyz = 'abc';

The above script resulted in an error on console window:

Uncaught SyntaxError: Unexpected token 'var'

The problem was near the closing curly brace near var xyz declaration as it has no space and it considers it as a wrong syntax. By adding some space and making sure there is semicolon at the end of each statement helped me clear the issues. Working script looked like this:

window.onload = function(){ var abc = 'xyz';}; var xyz = 'abc';
发布评论

评论列表(0)

  1. 暂无评论