开发者

How to check client-side JavaScript code for syntax errors? [closed]

开发者 https://www.devze.com 2023-02-16 14:02 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 7 years ago.

开发者_如何学C Improve this question

I have some client-side JavaScript code and want to check that file for syntax errors/warnings.

What is the easiest way to check my JavaScript file for "compile time" errors, such as a mismatched bracket or a keyword typo?


Copy and paste it into http://www.jslint.com/ but be prepared to "have your feelings hurt".


Opening your JavaScript console in the Chrome browser with (Tools->JavaScript console) or (CTRL + SHIFT + J) is always a good place to start. However, JSLint is really your best bet.


I'd reccomend trying out the Google Closure Compiler. Not only can it check your code for errors it can also perform some optimizations. "Compiler" here might be a bit of a misnomer since it takes in JavaScript and outputs back JavaScript.

To try it out you can copy and paste your code into the online Closure Compiler Service.

I noticed a lot of people suggesting JSLint. I just stumbled on JSHint from a tweet from John Resig that looks super flexible for enforcing coding conventions.


JSLint et all are great if you really worry about tabs and spaces, but for JS syntax I use http://esprima.org/demo/validate.html

I'm dropping some small JS/jQuery calls+functions into my php and the esprima checker fits the bill for me.


Use something like FireBug for Firefox.


Firebug for firefox:

Use Firefox browser to download and install the add-on: https://getfirebug.com/downloads/

If you have an older version of Firefox, click the download older versions, and single click on the .xpi file to install. Pick the one with the most recent date compatible with your version of firefox.

Put this code into a file called whatever.html:

<script type="text/javascript">
    var successFunction(){
    }
</script>

Open that file with firefox. Open the firebug window. Click the Console tab. Enable it. And it should tell you that the code above code has a error:

How to check client-side JavaScript code for syntax errors? [closed]


There are two ways to find issues:

  • Static analysis You could use jshint/jslint with gruntjs builder to identify mostly syntax issues. https://github.com/gruntjs/grunt-contrib-jshint

  • Runtime error management You can catch errors and logs from your clients with logging services like a JsLog.me. Each browser have its own runtime environment for JavaScript, so client logging catching real-world issues.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号