Packed/minified javascript failing in IE6 - how to debug?

怎甘沉沦 提交于 2020-01-17 05:35:19

问题


I have a number of files which I combine and pack to produce a single, minified JS file. The problem is that when I minify the file (using packer), IE6 gives one of its characteristic helpful error messages.

Line: 12      // of course, line 12 is an empty line
Char: 1
Error: Expected ')'
Code: 0

The thing is: it works fine in IE7, Firefox and Chrome the problem only comes up for IE6.

Unpacked, I have almost 200kb of scripts spread through 8 files. How on earth do I fix this?


回答1:


Microsoft have their way of doing it:

http://blogs.msdn.com/ie/archive/2004/10/26/247912.aspx

http://www.jonathanboutelle.com/mt/archives/2006/01/howto_debug_jav.html




回答2:


Like CMS said, the YUI compressor is a great tool to compress and obfuscate your code, try that.

I use the following code on my javascript files. I'm running on OSX, but the command should be identical on Linux and possibly also on Windows (though I never tried).

java -jar /path/to/yuicompressor-2.4.jar --charset utf8 -o ~/path/to/scriptname.min.js ~/path/to/scriptname.js 

Where ~/path/to/ is the path to wherever your javascript file is, scriptname.min.js is the name of the minimized/obfuscated end result, and scriptname.js is the original file.

I'm assuming you cannot just 'forget' about IE6? One of my new year wishes is that the last 23% of IE6 users on the internet finally upgrade to a more decent/up-to-date browser :-).

Hope this helps!

-Dave




回答3:


this is a very common problem with ie6, you have to pay attention to the closures in your code,

the condition statements must be with { - } ... and function too.

if(){
}

function(){
};

you must put ; on the end of each statement , if not , the lines will merge into something that the browser cannot understand.

i use jslint.com for javascript debugging. look for the "missing semicolon" in the error list.




回答4:


Have you already verified that the un-minified code can run successfully in IE6? If so, because this is a syntax error and not a runtime error, the next step I'd take is start halving the javascript, minifying it, and seeing when the problem stops being reported. Then continue binary searching from there.




回答5:


From memory, I think the way I solved this problem in the end, and the way I generally tackle problems with minified code now, is to run it through JSLint. Its extraordinary strictness will pick up problems (eg missing semicolons) that don't cause problems on non-minified code. You'll have to trudge through a lot of not-really-an-error messages, but the problem will be in there somewhere!



来源:https://stackoverflow.com/questions/419094/packed-minified-javascript-failing-in-ie6-how-to-debug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!