jquery - find error in javascript - Stack Overflow

I have a few javacsript files, and I'm using a js packer to pack them, then bine them all and incl

I have a few javacsript files, and I'm using a js packer to pack them, then bine them all and include them in a page.

The problem is that after they are packed I'm getting this error:

Error: missing ; before statement

I assume it's because somewhere in the js file a new line is used instead of the ; character, and since the packer removes new lines you get the error

so, how could I find where ; is ommited in the script(s)?

I have a few javacsript files, and I'm using a js packer to pack them, then bine them all and include them in a page.

The problem is that after they are packed I'm getting this error:

Error: missing ; before statement

I assume it's because somewhere in the js file a new line is used instead of the ; character, and since the packer removes new lines you get the error

so, how could I find where ; is ommited in the script(s)?

Share Improve this question asked Jan 24, 2011 at 15:55 AlexAlex 66.1k185 gold badges459 silver badges651 bronze badges 3
  • 2 Pass the unpacked code trough JSLint. It will tell you where to put your semi colons. However, it may also indicate dozens of other errors :) – Šime Vidas Commented Jan 24, 2011 at 16:00
  • You may want to switch pressors. YUI Compressor is good, but Google Closure Compiler may be better. Here's a dated article on the subject: bloggingdeveloper./post/…. – Brian Donovan Commented Jan 24, 2011 at 16:07
  • thank you. found it :D (it was the cycle jquery plugin btw) – Alex Commented Jan 24, 2011 at 16:44
Add a ment  | 

3 Answers 3

Reset to default 4

There exists a tool called jslint which can statically analyse JavaScript source code with many option. It should tell you where the failure is. There is also an online version available. Check it out: http://www.jslint./

Depending on the tool you're using, this may happen. Imagine two .js files:

a.js

(function() {
    var bar = 10;
}())

b.js

var foo = 5;
alert(foo);

Both would work separatly, but if you pack them together, it wouldn't work anymore:

(function() { var bar = 10; }())var foo = 5;alert(foo);

obviously, because there is a missing ;. A good pattern to avoid that is to start every javascript file with a ;, like:

fixed a.js

;(function() {
    var bar = 10;
}())

fixed b.js

;var foo = 5;
alert(foo);

output

;(function() {var bar = 10;}());var foo = 5;alert(foo);

All clear, thanks!

Do the files give you an error when you use them without packing? Some packers require you to place a semicolon when defining functions using a literal, and also when using object literals (otherwise they generate incorrect code -- see this):

var func = function() {
...
}; //<--- semicolon required!

var obj = {
...
}; //<--- semicolon required!

What packer are you using (JSMin and Packer don't like it if you have missing semicolons)? You can also try running your file through JSLint to see where the error exists. I suggest running the unpacked version through JSLint first (so that you can tell if there is an error with your unpacked version).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744205094a4563062.html

相关推荐

  • jquery - find error in javascript - Stack Overflow

    I have a few javacsript files, and I'm using a js packer to pack them, then bine them all and incl

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信