53

I fixed this by simply adding var alert; However, is this what I should be doing to get the pesky error message to go away? Here is the fix. Here is the fail on www.jshint.com.

I'm trying to learn from the error it throws..not necessarily make them go away.

(function () {

"use strict";

var alert;  //  added this in to fix

function initialize_page()
  {
  alert ("hi");
  }

addEventListener('load', initialize_page);

})();
6
  • 2
    @zzzzBov scottlogic.co.uk/2011/03/jslint-vs-jshint Commented Nov 17, 2011 at 20:01
  • 1
    jslint is a syntax checker and it gives you feedback. If you use vim I recommend you install it.
    – puk
    Commented Nov 17, 2011 at 20:01
  • 1
    I ran your code without any problems
    – puk
    Commented Nov 17, 2011 at 20:02
  • @epascarello, good read, however i agree with Crockford's quote.
    – zzzzBov
    Commented Nov 17, 2011 at 20:03
  • @zzzzBov Some people drink Crockford's KoolAid, some people don't. :) Commented Nov 17, 2011 at 20:48

9 Answers 9

69

Instead of

alert('message')

you should use

window.alert('message');

Because this method is defined in window object.

This of course assumes you have browser option set to true in your .jshintrc, so this way jshint will know window object is exposed.

"browser"       : true,     // Standard browser globals e.g. window, document.

*The same thing happens with confirm().

2
  • 2
    This is the only solution that keeps browser:true. In my opinion, that makes this the best solution.
    – Perry Tew
    Commented Feb 19, 2016 at 10:58
  • I concur, this is the immediate solution. Commented Mar 9, 2018 at 3:33
63

This documentation says the following about the browser option:

This option defines globals exposed by modern browsers: all the way from good ol' document and navigator to the HTML5 FileReader and other new developments in the browser world. Note: this option doesn't expose variables like alert or console. See option devel for more information.

and the following about the devel option:

This option defines globals that are usually used for logging poor-man's debugging: console, alert, etc. It is usually a good idea to not to ship them in production because, for example, console.log breaks in legacy versions of Internet Explorer.

You have browser enabled and devel disabled. You can control these with checkboxes under "Assume" on the jshint original page. I also recommend heeding the warning in the documentation ;-)

3
  • I just click the development box on jshint.com, the options must be for the non-online version.
    – user656925
    Commented Aug 15, 2012 at 21:49
  • In JSLint for Visual Studio, the checkbox value is "Assume development globals"
    – Jowen
    Commented Oct 25, 2013 at 14:57
  • You can also set this option in your local .jshintrc file.
    – Spencer
    Commented Jul 16, 2014 at 0:19
25

Set "devel:true" in the Options. This enables things like Alert, console, etc.

See documentation here: http://jshint.com/docs/options/

1
  • Thank you, this was my issue and adding this config option fixed it immediately!
    – cfx
    Commented Oct 8, 2013 at 23:51
2

Use .jshintrc file or CTRL + , in VS Code, to edit options for jshint.

in js alert(data.status); or window.alert(data.status);

"window": true,
"alert": true

or best

"devel": true,

{
"esversion": 6,
"browser": true,
"undef": true,
"varstmt": true,
"forin": true,
"unused": true,
"funcscope": true,
"lastsemic": true,
"moz": true,
"jquery": true,
"module": true,
"devel": true,
"globals": {
    "window": true,
    "document": true,
    "console": true,
    "alert": true
}

}

0
function prod(arr) {
    let largest = 0;
    let secondLargest = 0;
    const len = arr.length;
    for (let i = 0; i < len; i++) {
        if (arr[i] > largest) {
            secondLargest = largest;
            largest = arr[i];
        }
        else if (arr[i] < largest && arr[i] > second_largest) {
            secondLargest = arr[i]
        }
    }
    return largest * secondLargest;
}

console.log(prod([2, 4, 7, 8]));
0
0

in Visual Code, for individual file or without .jshintrc add globals name

"use strict";
/* globals alert */
-2

Instead of:

alert('message')

I use:

var alert = window['alert'];
-5

try passing window object in :

(function (global) {

"use strict";

var alert;  //  added this in to fix

function initialize_page()
  {
  global.alert ("hi");
  }

addEventListener('load', initialize_page);

})(window);
3
  • Needs to set devel:true, not just browser:true. See documentation here: jshint.com/options
    – Joshua
    Commented Nov 17, 2011 at 20:03
  • 1
    This is a very ugly solution to such a small problem.
    – mgol
    Commented Aug 14, 2012 at 15:44
  • This can be done with the devel: true of jshint directive, instead of adding additional code Commented May 8, 2014 at 20:58
-7

declare alert as variable and it will work without any settings:

for example:

var alert;

alert('hello world');