why is the javascript method confirm() displayed as being undefined and listed as an error, when it is a JS method

4,605 views
Skip to first unread message

Jonathan Marzullo

unread,
Jul 6, 2013, 11:38:26 AM7/6/13
to jsh...@googlegroups.com
Hello,
  • Why is the Javascript method confirm() displayed as being undefined, and listed as an error, when it is a JS method?
  • Is there a way to make it so JSHINT understands this method as being part of JS?
thanks to all who reply!

Anton Kovalyov

unread,
Jul 6, 2013, 7:29:19 PM7/6/13
to jsh...@googlegroups.com
Globals such as confirm are available only in the browser environment. They're not available, for example, in Node. That's why JSHint complains about them by default. You can enable 'browser' option to tell JSHint that you intend to run your code in the browser environment:

/*jshint browser:true */

That should fix your problem.

Anton

-- 
You received this message because you are subscribed to the Google Groups "JSHint" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jshint+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Marzullo

unread,
Jul 6, 2013, 7:42:03 PM7/6/13
to jsh...@googlegroups.com

thank you for the quick reply!

- what checkboxes are checked and used when a reviewers is reviewing js/jquery code?

-also JSHINT throws errors about variables that are defined in a global scope elsewhere in another javascript file.. will these be errors be ignored by the reviewer since JSHINT can only check the variables that are present on the code its testing against?

thanks again for your help!!

You received this message because you are subscribed to a topic in the Google Groups "JSHint" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jshint/0rp0sXXLU9c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jshint+un...@googlegroups.com.

Anton Kovalyov

unread,
Jul 6, 2013, 7:52:27 PM7/6/13
to jsh...@googlegroups.com
You should ask people who will be reviewing your code, this mailing list is regarding JSHint questions only.

Anton

Jonathan Marzullo

unread,
Jul 6, 2013, 7:53:25 PM7/6/13
to jsh...@googlegroups.com

ok thanks again for all your help!!!

Jonathan Marzullo

unread,
Jul 6, 2013, 8:07:32 PM7/6/13
to jsh...@googlegroups.com

im sorry.. but even having browser: true (checked).. JSHINT still throws confirm() as an error and being undefined

Peter deHaan

unread,
Jul 6, 2013, 8:22:58 PM7/6/13
to jsh...@googlegroups.com
This works on JSHint.com if I have "Development" selected:

// Your code goes here.
alert("a");
confirm("b");
prompt("c");


You can find more info in the docs at http://jshint.com/docs/#environments

-peter


Jonathan Marzullo

unread,
Jul 6, 2013, 8:33:56 PM7/6/13
to jsh...@googlegroups.com
ok, that did the trick! ..  thank you very much!!!


~ Jonathan

If you make it, they will break it!

Peter deHaan

unread,
Jul 6, 2013, 8:41:38 PM7/6/13
to jsh...@googlegroups.com
As for your other question, you can specify global variables which aren't defined in the current document. Check out the docs at http://jshint.com/docs/#directives if you want to specify the global variables in your JavaScript files:

/* global MY_LIB: false */

Or you can also define globals if you're calling JSHint() directly using Node.js or whatever:

var success = JSHINT(source, options, globals);

-peter

Jonathan Marzullo

unread,
Jul 6, 2013, 8:59:17 PM7/6/13
to jsh...@googlegroups.com
thanks for the help!! ..  but doesn't this seem like more time to spend just to specify global variables, just for the sake of passing JSHINT, to get a theme approved when reviewed ?

THANKS AGAIN FOR YOUR VERY FAST REPLY!!!!


~ Jonathan

If you make it, they will break it!


Jonathan Marzullo

unread,
Jul 6, 2013, 10:15:53 PM7/6/13
to jsh...@googlegroups.com
also i notice that JSHINT even when jquery is set to true (jquery:true).. it throws an error when a variable is inside the jquery dom ready() method.. displaying that variable as being undefined... for example:

jQuery("button").on("click", function(){
       index =  jQuery('#input2").val();
});

jQuery(document).ready(function(){
      var index = jQuery('#input1").val();
});


In this case JSHINT will throw an error displaying that index is not defined. JSHINT doesnt take into account the jquery ready() method that is run when the DOM is ready. I understand about defining the variable before the ready() method.. but sometimes an inputs value is not accessible until the dom is ready.. especially if the script tag is loaded into the HEAD tag.

any help regarding this JSHINT behaivor throwing this error will be highly appreciated.. !!!



~ Jonathan

If you make it, they will break it!


Peter deHaan

unread,
Jul 6, 2013, 11:36:00 PM7/6/13
to jsh...@googlegroups.com
Yep, that sounds about right. Consider the following code:

// Your code goes here.
jQuery("button").on("click", function(){
       index =  jQuery("#input2").val();
});

jQuery(document).ready(function(){
      var index = jQuery("#input1").val();
});


JSHint.com will give you two errors:
1) Line 3: "index" is not defined.
2) Line 7: "index" is defined but never used.

The problem is that your $(document).ready() function is creating a local variable named "index" which isnt in scope when the button click event handler gets called. So you're defining one `index` variable that gets defined but never used (in ready()), and then trying to access an `index` variable that isn't defined in the click handler.

One solution would be to move the `index` variable outside of the `ready()` function declaration into the global scope:

// Your code goes here.
var index;
jQuery("button").on("click", function(){
       index =  jQuery("#input2").val();
});

jQuery(document).ready(function(){
      index = jQuery("#input1").val();
});

JSHint Report


/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, undef:true, unused:true, curly:true, browser:true, devel:true, jquery:true, node:true, indent:4, maxerr:50 */

Errors:

  • Line 3index = jQuery("#input2").val();

    'index' is not defined.

  • Line 7var index = jQuery("#input1").val();

    'index' is defined but never used.



Jonathan Marzullo

unread,
Jul 7, 2013, 12:00:25 AM7/7/13
to jsh...@googlegroups.com

thanks again for the reply..

I usually define variables before ready() .. but didnt know why jshint was throwing those errors since when the browser runs the code it runs everything outside ready() until the dom is ready.. and then runs whats inside ready() .. thanks!

- do you know when a THEME reviewer is testing your js code in JSHINT.. what checkboxes they have checked when running it through JSHINT ?? .. or is it dependant on the JSHINT comments we have inside the code being tested ??

- also I noticed that if you run minified js code through JSHINT.. that it will throw errors about missing semicolons.. not sure why ??

thank you so much again for how fast and all your help and answering my questions !!  :)

Peter deHaan

unread,
Jul 7, 2013, 12:11:57 AM7/7/13
to jsh...@googlegroups.com
I don't know what a THEME reviewer is, sorry, but I'd assume if you want to just put your settings in a comment at the top of the JavaScript file if thee reviweer person is manually testing the code using JSHint.com.


/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, undef:true, unused:true, curly:true, browser:true, devel:true, jquery:true, node:true, indent:4, maxerr:50 */


Or if they are using Node.js+JSHint, you may want to add a .jshintrc file in your project directory instead if you have multiple .js files and don't want to repeat the options in each file.

-peter


Jonathan Marzullo

unread,
Jul 7, 2013, 12:26:55 AM7/7/13
to jsh...@googlegroups.com

ok thanks..

A theme reviewer is a Envato Themeforest reviewer that looks over a WordPress theme that is submitted to be sold on Themeforest.net.

Envato Marketplace is making it a requirement now that WordPress themes submitted, have their custom JS be JSHINT compliant.

so it looks like alot more people will be using your great JS quality tool.

I appreaciate all your help and for how fast you answered all my questions!

thank you and have a great night!

Jonathan Marzullo

unread,
Jul 7, 2013, 2:54:27 PM7/7/13
to jsh...@googlegroups.com
hello .. i had two more question..

- How do i configure JSHINT to not throw an error for Missing semicolon on minified JS/jQuery code?

- Also how to configure for JSHINT not to throw errors when i use a closure like below ?

(function(){

     // code goes here

})(jQuery);

thank you again for all your help!!!


~ Jonathan

If you make it, they will break it!


Peter deHaan

unread,
Jul 7, 2013, 3:11:27 PM7/7/13
to jsh...@googlegroups.com
a) I typically don't run JSHint against minified code, only raw source code.
b) I don't get any errors when linting that code using jshint.com and making sure "jQuery" checkbox is selected:

// Your code goes here.

Jonathan Marzullo

unread,
Jul 7, 2013, 6:35:39 PM7/7/13
to jsh...@googlegroups.com

weird cuz I had jquery checked.. thanks I will run my code again with that closure wrapped around it and see if I see that error again.. if I do I will copy / paste the error given in a reply back..

THANKS AGAIN.. have a nice day!!

Gajendra KN

unread,
Mar 14, 2017, 8:49:08 AM3/14/17
to JSHint
Hey,

Could you let me know what changes were implemented to fix confirm is undefined in jshint error.
Reply all
Reply to author
Forward
0 new messages