75

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bjorn
  • 69,215
  • 39
  • 136
  • 164
  • How do you use window? You could probably remove the dependency upon it, although bjoernwibben's solution below seems to do the job. – Guðmundur H Sep 10 '09 at 08:39
  • Well I was using it for window.setTimeout. I know I don't have to that and probably shouldn't, but in some places where I work with multiple windows in a Firefox extension I would need access to it. Thanks! – Bjorn Sep 10 '09 at 08:45
  • Obligatory [JSHint](http://www.jshint.com) comment. It addresses some of these JSLint issues, and is much prettier IMHO. ;) – user256430 Feb 27 '14 at 22:03

5 Answers5

135
/*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 */
Max
  • 386
  • 3
  • 16
Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
  • @bjoernwibeen has said in his answer that this is the correct solution as of now. Note, that there was two years difference in both of our answers. – Matt Clarkson Feb 17 '14 at 10:03
  • 1
    The official documentation to support this can be found at: http://www.jslint.com/lint.html#options `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.` This also includes window. – Koen Zomers Mar 03 '14 at 15:59
  • Marked it as the correct answer. Should have added the comment on the question so I would have gotten an alert not fixed this 3 years too late. :P – Bjorn Sep 22 '14 at 18:09
  • 1
    @BjornTipling, sorry. I now know to ping a comment on the question in future! – Matt Clarkson Sep 29 '14 at 14:39
  • @ryanpcmcquen, the [JSLint manual](http://www.jslint.com/help.html#global) stills says that it is the correct way to do it. The jslint.com website even ignores the 'a browser' checkbox at the moment. – Matt Clarkson Oct 29 '15 at 14:18
  • @MattClarkson, it appears that both are required (for me at least). – ryanpcmcquen Oct 29 '15 at 16:07
  • The solution (as of July 2017) is `/*global window: false */` – eli-bd Jul 06 '17 at 05:58
76

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.

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
bjoernwibben
  • 1,561
  • 13
  • 7
4

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.

akivajgordon
  • 8,761
  • 1
  • 19
  • 27
3

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*/
ryanpcmcquen
  • 6,285
  • 3
  • 24
  • 37
1

If you dont want to specify this in every file you can set it globally in your eslintrc config file like this:

"globals": {
    "window": true,
}
nilsi
  • 10,351
  • 10
  • 67
  • 79