Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: dynamic event handlers


Message #1 by "Chris Herring" <cherring@c...> on Thu, 28 Jun 2001 19:22:01
Good day! I am confused about why this works the way it does. If you load 
the following page (on IE5) and roll your mouse over the "test text", you 
should get two successive dialg boxes. The first will show the 'value' of 
the onmouseover handler, and the second shows the color value passed to 
the menuOver function. What has me stumped is how the color value 
of "#222222" is passed to the menuOver function, when it appears that the 
handler is passing what should now be an undefined variable (sNewColor). 
It seems the first dialog box should return something like "function()
{menuOver(this,'#222222');", but in order to make that happen I would have 
to write something like "onmouseover=function()(menuover(this,eval
(sNewColor)));" but of course that does not work.. Is there a better or 
different way to assign the event handler?

Thanks for your patience.. 

Chris Herring

here is the code (probably only works on IE 5.5..)

<html>
<head>
<script>
	function startup(){
		addMenu('testMenu','#222222');
	}
	function addMenu(sId,sNewColor){
		var oDivShell=document.createElement('div');
		with (oDivShell){
			id=sId;
			innerHTML='test text';
			onmouseover=function(){menuOver(this,sNewColor)};
		}
		document.body.appendChild(oDivShell);
	}
	function menuOver(oMe,sColor){
		alert(oMe.onmouseover);
		alert(sColor);
	}
</script>
</head>
<body onload="startup();"style="background-color:white">
</body>
</html> 

  Return to Index