Don't make functions within a loop. error from JSLint
I am working on a site and in the process borrowed some js from some one to get dropdown menus to work, but after a while got reports from people who tested my site of some problems and decided to verify all my code, and i have fixed most bugs but the one listed in the title.
Tell jslint to jump in the lake. That's a perfectly reasonable way of coding what is being done there.
It *is* true that you don't *need* to make those particular functions in a loop, but there are other similar functions that you would need to do just that way.,
You should be able to get rid of the other message by just changing
Code:
sfHover = function() {
to
Code:
function sfHover() {
though again I think jslint is just being anally retentive.
Oh, w.t.h. If you want to move the function-building out of the loop:
Code:
function navOver() { this.className += " sfhover"; }
function navOut() { this.className = this.className.replace(new RegExp(/ sfhover\b/, ""); }
function sfHover() {
var sfEls = document.getElementById("nav").getElementsByTagName("LI");
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover = navOver;
sfEls[i].onmouseout = navOut;
}
}
if (window.attachEvent) {window.attachEvent("onload", sfHover);}
Just out of curiosity, what do you do if window.attachEvent is *NOT* defined? (i.e., if its MSIE.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
functions in a loop are the only way to close certain values.
JSLint is good for corralling noobs to stop writing old-fashioned code styles (c, java, etc) but it has no place in a serious development workflow.
even crappy web pages validate; doesn't amount to a hill of beans for anyone beside the coder and a supervisor...
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
Thanks guys, and I will be the first to admit i am a noob with web programming, especially when it comes to js. just know that when i have goon to css forums for help there they have asked me to validate the code. but i seem to have it working, so thanks for your help.