JSLint's issue with 'window' as a global variable

So I'm using JSLint to try and detect errors. I turn some options off I don't like, but I don't see any way to enable being able to use the window global variable. Well, there is the Yahoo Widget option, but that's overkill.

What's the deal with using 'window', why would JSLint say that is causing errors?


Solution 1:

/*jslint browser: true*/

Was the correct solution to this. As of 2017-07-07, you have to set the global directive manually. From the JSLint documentation:

The /*global*/ directive is used to specify a set of globals (usually functions and objects containing functions) that are available to this file. This was commonly used in browsers to link source files together before ES6 modules appeared. Use of global variables is strongly discouraged, but unfortunately web browsers require their use. The /*global*/ directive can only be used when the Assume a browser option is selected.

So you will need to use:

/*jslint browser */
/*global window */

Solution 2:

Just make a comment in your script like that:

/*global window */

... your script goes here

This comment will tell JSLint that window is defined somewhere else.

See: http://www.JSLint.com/lint.html,

JSLint also recognizes a /* global */ comment 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 indicated that the variable may be assigned to by this file, and false indicating that assignment is not allowed which is the default.

When you want window to be global by default without having to apply the comment to your script, you can add predef:["window"] to the object literal parameter inside the JSLINT function of your local jslint.js file.

BTW, I'm using predef:["$","window"] to have jQuery global as well.

Update:

This answer was correct back in 2009. As of now you should use the solution /*jslint browser: true*/ provided by Matt Clarkson.

Solution 3:

To let JSLint know that you recognize window as a global object, add this directive at the top of your file:

/*global window*/

I used to be able to use:

/*jslint browser: true */

but this no longer seems to work. Now, according to the JSHint help regarding the browser option:

It does not supply self or window; you will have to request these aliases of the dreaded global object yourself.

I'm not sure when that change was made, but it had me stymied for a while.

Solution 4:

I had to use both of the above answers on this code to get rid of all warnings:

/*jslint browser:true*/
/*global window*/
// eventBoiler v0.1.1 by @ryanpcmcquen
// https://github.com/ryanpcmcquen/eventBoiler
(function (win, doc) {
    'use strict';
    win.eventBoiler = function (selector, typeOfEvent, func) {
        doc.querySelector(selector).addEventListener(typeOfEvent, func);
    };
    win.eventBoiler.all = function (selectors, typeOfEvent, func) {
        Array.prototype.slice.call(doc.querySelectorAll(selectors)).map(function (i) {
            i.addEventListener(typeOfEvent, func);
        });
    };
}(window, document));

So for me, this is the solution when using the JSLint website:

/*jslint browser:true*/
/*global window*/