Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to validate my jQuery JavaScript with JSLint?

I wanted to check my JavaScript with JSLint. I am also using jQuery and JSLint seems to be very unhappy about jQuery.

So, if I have this code:

$(document).ready{
    $("a").click(function() {
        // foo
    });
});

I am getting a few JSLint messages:

Error:

Problem at line 1 character 1: '$' was used before it was defined.

$(document).ready{

...

I had a look at the options but I couldn't figure out how to tell JSLint that this is ok.

If I had only one jQuery call in my code I could just ignore the JSLint message but in 2,500 lines of script there are lots of calls, I can't find the errors I'd like to fix between all these messages.

So, does anyone know how to configure JSLint such that it works with jQuery calls? Or is there something else I can use to check the quality of my JavaScript/jQuery code?

like image 764
newtogit Avatar asked Nov 01 '10 17:11

newtogit


2 Answers

That's not valid JavaScript, it should be:

$(document).ready(function() {

So JSLint will (appropriately) complain about your syntax.

If you want to get rid of the Implied global: $, document message, go to the bottom where it has Predefined (, separated), and put jQuery, $ in that textbox then run again. For the document piece, check Assume a browser in the first column.

All JSLint settings will stick, so you don't have to do this each time you go back.

like image 93
Nick Craver Avatar answered Nov 15 '22 10:11

Nick Craver


Add this at the top of your document to get it to validate:

/*jslint browser: true*/ /*global  $*/
like image 21
Andy Balaam Avatar answered Nov 15 '22 09:11

Andy Balaam