nick wrote:
[Pretty-printed the source code]
var ocevent = function(v)
{
alert('javascript event: u clicked '+v);
return false;
};
It would be both more reasonable and downwards compatible to write
function ocevent(v)
{
alert('javascript event: u clicked ' + v);
return false;
}
var items = {
"002.jpg": {text:"002", href:"#", click:function(){return ocevent(2)} },
^^^^^^^^
What will users do without support for client-side scripting?
"003.jpg": {text:"003", href:"#", click:function(){return ocevent(3)} },
...}
It would be more downwards compatible if you used both the Object and
Function constructors:
var items = new Object();
items["002.jpg"] = new Object();
items["002.jpg"].text = "002";
items["002.jpg"].href = "#"; // TODO!
items["002.jpg"].click = new Function("return ocevent(2);");
items["003.jpg"] = new Object();
items["003.jpg"].text = "003";
items["003.jpg"].href = "#"; // TODO!
items["003.jpg"].click = new Function("return ocevent(3);");
However, whether you want that depends on the (age of) UAs you want
to support. Object literals require at least JavaScript 1.1 or an
ECMAScript 3 compliant language implementation; the `function'
operator is available from JavaScript 1.5 and ECMAScript 3 on.
And I use the following code to assign the click property to onclick of
anchor.
a.setAttribute('onclick', items[id].click);
It is because you are assigning a Function object reference to an
attribute where a (DOM) string is expected. It is not a bug in
setAttribute() in Firefox, it is your misuse of that method and
probably a weird implementation in IE to convert the reference
into a string (by calling the toString() method of the referenced
Function object). Either pass a string
a.setAttribute('onclick', "return ocevent(3)");
or assign the function reference to the `onclick' property of the object
referenced with "a":
a.onclick = items[id].click;
However, I do not know if the former with IE, in fact I am almost certain
it does not work with IE prior to version 5 (as those versions do not
support the W3C DOM); the latter technique on the other hand should work
with all browsers that support client-side scripting (taking into account
that `a' refers to an HTMLAnchorElement object or its equivalent).
However, the recommended standards compliant technique as of the W3C DOM
Level 2 Events Specification is
a.addEventListener('click', function() { return ocevent(3); }, false);
See also registerEvent() in said:
Any suggestion for function pointers?
See above. As for the latter, there are no function pointers, since
there are no (accessible) pointers, in JavaScript 1.x and ECMAScript
implementations.
PointedEars