Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-30-2010, 03:08 AM   PM User | #1
river226
New to the CF scene

 
Join Date: Aug 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
river226 is an unknown quantity at this point
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.

Code:
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		};
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		};
	}
};
if (window.attachEvent) {window.attachEvent("onload", sfHover);}
and i am verifying it on jslint

the error i get is:

Quote:
Error:

Problem at line 6 character 10: Don't make functions within a loop.

};

Problem at line 9 character 10: Don't make functions within a loop.

};

Implied global: sfHover 1,12, document 2, window 12
any help will be appreciated, and I'm sure this isn't complex but i don't know much js and want to keep it the same.
river226 is offline   Reply With Quote
Old 08-30-2010, 04:23 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 08-30-2010, 04:48 AM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
// You need a better event handler, yours only works in IE.

Code:
window.handler= (function(){
	if(window.addEventListener){
		return function(who, what, fun, mod){
			mod= !!mod;
			who.addEventListener(what, fun, mod);
		}
	}
	else if(window.attachEvent){
		return function(who, what, fun){
			who.attachEvent('on'+what, fun);
		}
	}
})()
// If you set the event to fire when it bubbles up,
// you only need one event handler for the whole list.

Code:
sfHover= function(e){
	e= window.event? event: e;
	var t= e.type.toLowerCase()== 'mouseover', 
	who= e.target || e.srcElement,
	c= who.className.replace(/ *sfhover */,'');
	if(who.tagName== 'LI'){
		if(t) c= c? c+" sfhover": '';
		this.className= c;
	}
}

Code:
handler(window,'load', function(){
	var pa= document.getElementById("nav");
	handler(pa,'mouseover', sfhover);
	handler(pa,'mouseout', sfhover);
});
mrhoo is offline   Reply With Quote
Old 08-30-2010, 06:39 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yeah, nice way, Hoo.

Do you agree, though, that jslint is being overly picky for no good purpose??
__________________
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.
Old Pedant is offline   Reply With Quote
Old 08-30-2010, 02:25 PM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by mrhoo View Post
// You need a better event handler, yours only works in IE.
that code is the Suckerfish Dropdown fix for IE to allow :hover on elements other than <a> (i.e. <li>). It doesn’t need to run in other browsers.
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 08-30-2010 at 02:30 PM..
Dormilich is offline   Reply With Quote
Old 08-30-2010, 09:00 PM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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%

Last edited by rnd me; 08-30-2010 at 09:09 PM..
rnd me is offline   Reply With Quote
Old 08-30-2010, 09:52 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Thank you, RndMe, for confirming my assertions.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 09-06-2010, 11:25 PM   PM User | #8
river226
New to the CF scene

 
Join Date: Aug 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
river226 is an unknown quantity at this point
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.
river226 is offline   Reply With Quote
Reply

Bookmarks

Tags
error, funtion within loop, jslint

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:39 PM.


Advertisement
Log in to turn off these ads.