95

Hi I have the 3 javascript files.

  • jquery.js
  • utility.js
  • file1.js

In file1.js I have

jQuery.noConflict()
jQuery(document).ready(function($) { 
 // ....
});

I get an error 'jQuery' was used before it was defined. and 'document' was used before it was defined.

How do I safely get rid of this warning.

If I do

var document = document || {}; 

then in my utility.js if it is used, it would be null in IE and ok in firefox.

What is the best solution to this?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Chun ping Wang
  • 3,879
  • 12
  • 42
  • 53
  • possible duplicate of [JS Lint: 'shortcut' was used before it was defined](http://stackoverflow.com/questions/8134049/js-lint-shortcut-was-used-before-it-was-defined). Also, make sure you have *Assume browser* selected in the options. – Andy E Mar 08 '12 at 16:33
  • @ChunpingWang Noticed that you were still active on SO and this question was missing an accepted answer. Are either of these answers missing something that's needed to make them acceptable? Seems like Quentin's answer would solve the problem as asked. Any additional information needed to answer your question? – ruffin Mar 11 '15 at 16:33

2 Answers2

169

From the documentation

JSLint also recognizes a /*global */ directive that can indicate to JSLint that variables used in this file were defined in other files. The comment can contain a comma separated list of names. Each name can optionally be followed by a colon and either true or false, true indicating that the variable may be assigned to by this file, and false indicating that assignment is not allowed (which is the default). The directive respects function scope.

Some globals can be predefined for you. Select the Assume a browser (browser) option to predefine the standard global properties that are supplied by web browsers, such as document and addEventListener.

Example:

/*jslint browser: true*/
/*global $, jQuery*/
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 25
    note: you can't put a space before `global` so `/* global var, var2 */` doesn't work. `/*global var, var2*/` works – radixhound Jul 27 '12 at 20:15
  • 4
    Plural is equivalent and works as well: `/*globals */` and also makes more sense when adding a list of variables – Robert Koritnik Aug 02 '12 at 13:40
  • 2
    So the solution to JSLint complaining about something missing from all of your files it to add a line to all of your files... This is terrible advice. – Cerin Feb 19 '16 at 21:50
  • @Cerin — No, the solution to JSLint complaining about you using a variable that it can't find a definition for is to tell it that you defined it in another file (as opposed to, for example, making a typo in the variable name that will break your program later). – Quentin Feb 21 '16 at 10:54
  • 1
    @Quentin, Yes, and your fix for that requires that you "tell it about jQuery" in every single file. That's an enormous amount of work for something that provides no benefit. You might as well simply disable that check in JSLint. – Cerin Feb 21 '16 at 19:50
  • @Cerin — And then you make a typo on some other variable name and JSLint doesn't warn you, and you spend ages debugging it. That isn't worth the trivial amount of effort required to mention the library files you are using at the top of a file before you lint it. – Quentin Feb 21 '16 at 19:55
  • @Quentin, link needs updating, its a dead link. – James Oravec Jun 21 '16 at 22:12
30

As Quentin says, there's a /*global*/ directive.

Here is an example (put this at the top of the file):

/*global var1,var2,var3,var4,var5*/

Make sure the initial global statement is on the same line as /*, or else it breaks.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
stanton
  • 439
  • 5
  • 5