[thelist] Trouble setting onClick dynamically in IE and FireFox

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Thu Apr 13 00:13:07 CDT 2006


Hi Chris,

I've slightly altered your code to make it more robust, leak-free and
unobtrusive:

<script type="text/javascript">
window.onload=function(evt) {
 var e = document.getElementsByTagName("a");
 for( var i=0; i < e.length; i++ ) {
    e[i].onclick = anchor_click; /*
  easiest way to prevent memory leak issues
  in MSIE is not to use a closure
  for more info:
  see the references at the end*/
 }
};

function anchor_click(evt){
 alert("you clicked me!");
 //return true;/*no need*/
}
</script>
<body>
<a id="a" href="page2.php" >Click here and you'll go crazy/a><br />
<a id="b" href="link1.html" >Click here and you won't</a>
</body>

References:

[1] Help! my browser leaks memory
http://www.volkanozcelik.com/volkanozcelik/blog/2005/10/help-my-browser-leaks-memory.html
[2] Memory leakage in IE revisited
http://www.codeproject.com/jscript/leakpatterns.asp
[3]
A simple leak-free XMLHttpRequest wrapper (partially related)
http://www.volkanozcelik.com/cre8/blog/2006/02/simple-leak-free-defensive.html
note:
I've not tested the code but it should work as expected.

HTH,
--
Volkan Ozcelik
+>Yep! I'm blogging! : http://www.volkanozcelik.com/volkanozcelik/blog/
+> My projects/studies/trials/errors : http://www.sarmal.com/



More information about the thelist mailing list