Re: Adding events in a loop with arguments



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
.



Relevant Pages

  • Re: Adding events in a loop with arguments
    ... but creates an additional execution object on the scope chain ... This can be avoided by setting ahrefs to null at the end. ... Other methods that don't have memory leak problems are: ... Use the built-in Function object as a constructor: ...
    (comp.lang.javascript)
  • Re: Adding events in a loop with arguments
    ... but creates an additional execution object on the scope chain ... Other methods that don't have memory leak problems are: ... Use the built-in Function object as a constructor: ... I'll look into closures and of course ...
    (comp.lang.javascript)
  • Re: What is a software engineer?
    ... Function - to create a minimal scope chain function that wrapped ... the (now not indirect) eval call, ... Recall that as it stands JQurey is wrapped in the inline execution of a function expression, which provides a 'private' 'global' scope for just the JQuery code. ...
    (comp.lang.javascript)