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)?
- 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
3 Answers
Reset to default 4There 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
评论列表(0条)