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

jquery - find error in javascript - Stack Overflow

programmeradmin5浏览0评论

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).

发布评论

评论列表(0)

  1. 暂无评论