Re: Adding events in a loop with arguments
- From: RobG <rgqld@xxxxxxxxxxxx>
- Date: Wed, 5 Dec 2007 17:09:11 -0800 (PST)
On Dec 6, 3:23 am, David Mark <dmark.cins...@xxxxxxxxx> wrote:
On Dec 4, 11:13 pm, Vivek <joshi...@xxxxxxxxx> wrote:
Hello I am trying to attach events in my application. These events
have dynamically created parameters. I am attaching the code for a
You mean listeners that are defined as function expressions.
simple app that demonstrates something similar to what i am trying to
achieve. I expect to see 1, 2 and 3 when i click on the links, however
all i see is 3, which is the final value for i. Can someone please
explain to me how to get around this issue. Thanks much
<html xmlns="http://www.w3.org/1999/xhtml" >
This is hardly XHTML. It isn't even valid HTML.
<head>
<title>js test page</title>
<script type="text/javascript">
function attachEvents()
{
var ahrefs = document.getElementsByTagName("a");
for(var i=0; i<ahrefs.length; i++)
ahrefs[i].onclick = function(){alert(i)};
The fact that you expect this to work indicates that you don't
understand closures.
http://www.jibbering.com/faq/faq_notes/closures.html
[...]
<script type="text/javascript">
var global = this;
Do you have any examples of where this is required or useful?
if (this.document && this.document.getElementsByTagName && this.alert)
{
this.onload = function() {
Why set "global" then not use it above?
var l;
var ahrefs = global.document.getElementsByTagName("a");
l = ahrefs.length;
while (l--) {
ahrefs[l].href = '#';
ahrefs[l].tabIndex = 0;
ahrefs[l].onclick = (function(j) { return function()
{ global.alert(j); return false; }; })(l + 1);
Wherever possible, assigning a function expression that forms a
circular reference should be avoided else IE's famous memory leak
becomes an issue. This particular form effectively breaks the closure
with i, but creates an additional execution object on the scope chain
that exacerbates the leak.
At least variables like ahrefs should be set to null or deleted.
Other methods that don't have memory leak problems are:
1. Use a function expression:
ahrefs[l].onclick = someFunction;
2. Assign the handler outside the scope of the main function like:
assignHandler(element, event, functionRef [, paramArray]);
3. Use the built-in Function object as a constructor:
ahrefs[1].onclick = new Function('...');
--
Rob
.
- Follow-Ups:
- Re: Adding events in a loop with arguments
- From: Thomas 'PointedEars' Lahn
- Re: Adding events in a loop with arguments
- From: David Mark
- Re: Adding events in a loop with arguments
- From: David Mark
- Re: Adding events in a loop with arguments
- From: Vivek
- Re: Adding events in a loop with arguments
- References:
- Adding events in a loop with arguments
- From: Vivek
- Re: Adding events in a loop with arguments
- From: David Mark
- Adding events in a loop with arguments
- Prev by Date: Re: opacity
- Next by Date: Re: escapes and JSON
- Previous by thread: Re: Adding events in a loop with arguments
- Next by thread: Re: Adding events in a loop with arguments
- Index(es):
Relevant Pages
|