Article ID: 183509
Article Last Modified on 5/11/2006
div1.onclick = OnClickHandler(str);
div1.onclick = new Function("return OnClickHandler(str)");
<HTML>
<HEAD>
<SCRIPT>
function InitPage()
{
str = "Hello, world";
// This causes the OnClickHandler to be called (Perceived Problem)
div1.onclick = OnClickHandler(str);
// This associates an event handler with an object (Solution)
// div1.onclick = new Function("return OnClickHandler(str)");
}
function OnClickHandler(str)
{
alert(str);
}
</SCRIPT>
</HEAD>
<BODY onload="InitPage()">
<P>Click the image to see the problem
<DIV ID=div1 STYLE="position:relative; height:100; width:100;
background-color:red">
</DIV>
</BODY>
</HTML>
Keywords: kbcode kbprb KB183509