How to rectify "Document was used before it was defined" using Jslint

starjava picture starjava · May 26, 2012 · Viewed 22.7k times · Source

I get the following error in jsLint:

'document' was used before it was defined.

Line that causes the error:

document.cookie = name + "=" + value + expires + "; path=/";

I get why this happens, but I would like my code to be compliant.

How do I resolve this?

Thanks.

Answer

penartur picture penartur · May 27, 2012

Place

/*jslint browser:true */

in the beginning of the body of your function. Or, alternatively, you may use

/*global document: false */

JSLint is for the checking of any javascript code, and the document global object does not exists everywhere, so you have to manually tell JSLint that your code is aimed to be executed in browser, and thus document global variable is defined. For example, for server-side javascript it is expected for JSLint to report about this error.