Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"alert" is not defined when running www.jshint.com

Tags:

javascript

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().


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 ;-)


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

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


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
}

}