473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

initiation not working

Hi Folk

Have a look at

http://www.corstorphine.co.nz/_quiz/quiz.php
Onload I do this:

function load_images() {
if (!check()) return;
var nav = document.getElementById('frm');
var imgs = nav.getElementsByTagName('img');
var x = 0;
for (var i =0;i<imgs.length;i++) {
imgs[i].number = i;
imgs[i].onclick = select_image;
var y = 'c' + x;
clicker[i] = new getObj(y);
clicker[i].obj.onclick = function () {tick_image(x);};
x++;
}

(see http://www.corstorphine.co.nz/_quiz/j.js)

However, if I make tick_image like this:

tick_image(x) {
alert(x);
}
then I always get 12 rather than a number between 0 and 11.

Just have a look at it, please, I cant really explain it all here, but
basically I dont understand why in the following line:

clicker[i].obj.onclick = function () {tick_image(x);};

x always becomes 12 while it clearly should be between 0 and 12.

TIA
Nicolaas

Mar 15 '06 #1
4 1159

windandwaves wrote:
Hi Folk

Have a look at

http://www.corstorphine.co.nz/_quiz/quiz.php
Onload I do this:

function load_images() {
if (!check()) return;
var nav = document.getElementById('frm');
var imgs = nav.getElementsByTagName('img');
var x = 0;
for (var i =0;i<imgs.length;i++) {
imgs[i].number = i;
imgs[i].onclick = select_image;
var y = 'c' + x;
clicker[i] = new getObj(y);
clicker[i].obj.onclick = function () {tick_image(x);};
x++;
}

(see http://www.corstorphine.co.nz/_quiz/j.js)

However, if I make tick_image like this:

tick_image(x) {
alert(x);
}
then I always get 12 rather than a number between 0 and 11.

Just have a look at it, please, I cant really explain it all here, but
basically I dont understand why in the following line:

clicker[i].obj.onclick = function () {tick_image(x);};

x always becomes 12 while it clearly should be between 0 and 12.

TIA
Nicolaas


You need to separate the assignment of functions when you want to pass
a variable to a function. For example:

for( etc. )
{
attachFunction( click[i], x );
}

....

function attachFunction(oClick, x)
{
oClick.onclick = function() { tick_image(x); };
}

Mar 15 '06 #2
web.dev wrote:
windandwaves wrote:
Hi Folk

Have a look at

http://www.corstorphine.co.nz/_quiz/quiz.php
Onload I do this:

function load_images() {
if (!check()) return;
var nav = document.getElementById('frm');
var imgs = nav.getElementsByTagName('img');
var x = 0;
for (var i =0;i<imgs.length;i++) {
imgs[i].number = i;
imgs[i].onclick = select_image;
var y = 'c' + x;
clicker[i] = new getObj(y);
clicker[i].obj.onclick = function () {tick_image(x);};
x++;
}

(see http://www.corstorphine.co.nz/_quiz/j.js)

However, if I make tick_image like this:

tick_image(x) {
alert(x);
}
then I always get 12 rather than a number between 0 and 11.

Just have a look at it, please, I cant really explain it all here,
but basically I dont understand why in the following line:

clicker[i].obj.onclick = function () {tick_image(x);};

x always becomes 12 while it clearly should be between 0 and 12.

TIA
Nicolaas


You need to separate the assignment of functions when you want to pass
a variable to a function. For example:

for( etc. )
{
attachFunction( click[i], x );
}

...

function attachFunction(oClick, x)
{
oClick.onclick = function() { tick_image(x); };
}


I have no idea why but it works! Would you care explaining it? It looks
great now!
Mar 15 '06 #3
On 15/03/2006 21:05, windandwaves wrote:
web.dev wrote:
windandwaves wrote:
[snip]
function load_images() {
if (!check()) return;
var nav = document.getElementById('frm');
var imgs = nav.getElementsByTagName('img');
var x = 0;
for (var i =0;i<imgs.length;i++) {
imgs[i].number = i;
imgs[i].onclick = select_image;
var y = 'c' + x;
clicker[i] = new getObj(y);
clicker[i].obj.onclick = function () {tick_image(x);};
x++;
}
[snip]
then I always get 12 rather than a number between 0 and 11.

[snip]
You need to separate the assignment of functions when you want to
pass a variable to a function.

[snip]
I have no idea why but it works! Would you care explaining it?


This is a rather fundamental feature of inner functions, and therefore
closures.

An inner function maintains a reference to the Variable object of each
outer function in a stack, ending with the global object. This is the
scope chain. The Variable objects it references contain local variables
as properties. It is the scope chain that is used to resolve identifiers.

Each time your loop executes, a new function object is created, and a
reference is assigned to the onclick property. While each object is new,
the scope chain is exactly the same. This means that each function
object shares the same set of variables as each one reference the same
Variable objects.

The conclusion? If one function changes the value of a variable, all of
these inner functions sees that change. Now, whilst your function
doesn't perform any modifications, the loop code does and the effect is
the same: by the end of the loop, each function will read the variable,
i, to be the value of img.length.

[snip]

By the way, you should consider removing those event listeners (assign
null to the onclick properties) when the document unloads to avoid
invoking IE's notorious memory leak.

You should also remove

clicker[i].obj.onclick = function () {tick_image(x);};

from the load_images function. It's redundant now.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 15 '06 #4
Michael Winter wrote:

[big snip and a big thank you for the exaplanation - I am slowly working out
that javascript treats variables very different from the other languages
that I know (VB + PHP)]
By the way, you should consider removing those event listeners (assign
null to the onclick properties) when the document unloads to avoid
invoking IE's notorious memory leak.
DONE!
You should also remove

clicker[i].obj.onclick = function () {tick_image(x);};

from the load_images function. It's redundant now.
YEP - thank you.

Nicolaas

Mar 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary | last post by:
I am trying to use the "System.Windows.Forms.SendKeys" class for triggering the Ctrl+P key. Syntax: System.Windows.Forms.SendKeys.Send("^(P)"); This is not working ..what could be the...
3
by: web1110 | last post by:
Hi y'all, How do I notify the system that a job is to be started up immediately upon reboot but only one time. SpyBot does this. When it finds trash it cannot remove, it can run first on a...
6
by: Mullin Yu | last post by:
hi, i have a web service that has file operations on Windows OS, and there may be a file concurrency issue if only one working directory e.g. c:\working therefore, i want to have a unique sub...
4
by: Nathaniel Sherman | last post by:
Alright, folks, here's the deal... I'm working on migrating a classic ASP website to an ASP.NET codebase. At the heart of the site is a MySQL database. To make sure an "in-process" program...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
5
by: tshad | last post by:
I have been working with setting my drop boxes to allow double clicking to select an item. It worked fine until I made some changes. I then stripped the page down to the bare essentials to find...
8
by: jojobar | last post by:
Okay, I am trying to do is to test the webresource in 2.0 1. I created a new project with assembly name (and default assembly name) "Office". 2. I added the following to the AssemblyInfo.cs...
2
by: Don | last post by:
I'm having problems with intellisense, autocomplete, etc. suddenly not working in certain classes of a project I'm working on. All the options are set, and it all works fine for most classes, but...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.