Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Mozilla doesn't handle function pointer well?

Reply
Thread Tools

Mozilla doesn't handle function pointer well?

 
 
nick
Guest
Posts: n/a
 
      09-30-2004
I have the following code:
var ocevent = function(v)

{

alert('javascript event: u clicked '+v);

return false;

};

var items = {

"002.jpg": {text:"002", href:"#", click:function(){return ocevent(2)} },

"003.jpg": {text:"003", href:"#", click:function(){return ocevent(3)} },

....}

And I use the following code to assign the click property to onclick of
anchor.

a.setAttribute('onclick', items[id].click);

However, the code works well in IE but not in Mozilla Firefox. To test the
code go to:

http://greywolfdesign.com/tmp/gallery/gallery.htm

Any suggestion for function pointers?


 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      09-30-2004
On Thu, 30 Sep 2004 11:15:25 -0400, nick <(E-Mail Removed)>
wrote:

[snip]

> And I use the following code to assign the click property to onclick of
> anchor.
>
> a.setAttribute('onclick', items[id].click);
>
> However, the code works well in IE but not in Mozilla Firefox.


That's because IE doesn't conform to standards. The setAttribute method
takes two string arguments, but IE allows any type for the second.

The solution is simple: don't use setAttribute. At all. All the attributes
you set in that code can, and should be, set using the relevant properties.

> Any suggestion for function pointers?


There's no such thing as a pointer in Javascript. They're references.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      09-30-2004
nick said:
>
>I have the following code:
>var ocevent = function(v)
>
>{
>
>alert('javascript event: u clicked '+v);
>
>return false;
>
>};
>
>var items = {
>
>"002.jpg": {text:"002", href:"#", click:function(){return ocevent(2)} },
>
>"003.jpg": {text:"003", href:"#", click:function(){return ocevent(3)} },
>
>...}
>
>And I use the following code to assign the click property to onclick of
>anchor.
>
>a.setAttribute('onclick', items[id].click);


A little testing shows that the problem isn't with function
references, but with the way you're assigning the onclick
event handler. Don't use setAttribute() to set event handlers.
Much simpler and more robust:

a.onclick=items[id].click;

 
Reply With Quote
 
nick
Guest
Posts: n/a
 
      09-30-2004
Thanks, right, the right term should be function reference, but quite a few
web site use term function pointer,

when i define the function reference in the following array, I had to wrap
the ocevent function with another anonymous function. Anyway to avoid it?
var ocevent = function(v)

{

alert('javascript event: u clicked '+v);

return false;

};

var items = {

"002.jpg": {text:"002", href:"#", click:function(){return ocevent(2)} },

"003.jpg": {text:"003", href:"#", click:function(){return ocevent(3)} },



"Michael Winter" <(E-Mail Removed)> wrote in message
newspse5cajqcx13kvk@atlantis...
> On Thu, 30 Sep 2004 11:15:25 -0400, nick <(E-Mail Removed)>
> wrote:
>
> [snip]
>
>> And I use the following code to assign the click property to onclick of
>> anchor.
>>
>> a.setAttribute('onclick', items[id].click);
>>
>> However, the code works well in IE but not in Mozilla Firefox.

>
> That's because IE doesn't conform to standards. The setAttribute method
> takes two string arguments, but IE allows any type for the second.
>
> The solution is simple: don't use setAttribute. At all. All the attributes
> you set in that code can, and should be, set using the relevant
> properties.
>
>> Any suggestion for function pointers?

>
> There's no such thing as a pointer in Javascript. They're references.
>
> Mike
>
> --
> Michael Winter
> Replace ".invalid" with ".uk" to reply by e-mail.



 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      09-30-2004
On Thu, 30 Sep 2004 11:56:17 -0400, nick <(E-Mail Removed)>
wrote:

> Thanks, right, the right term should be function reference, but quite a
> few web site use term function pointer,


Pointer is a term to be associated with memory addresses. As Javascript
doesn't expose that information, it isn't the right term to use.

> when i define the function reference in the following array, I had to
> wrap the ocevent function with another anonymous function. Anyway to
> avoid it?


If you want to pass an argument, what you've got is probably the most
reliable way. You'd probably have to restructure your code, completely. I
might be wrong, though.

[snip]

> "Michael Winter" <(E-Mail Removed)> wrote in message
> newspse5cajqcx13kvk@atlantis...


Please don't top-post. Write your responses in conversation order; placed
below the text you are directly responding to. Remove everything else
(preferably indicating when you do).

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-17-2004
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 <http://pointedears.de/scripts/dhtml.js>.

> 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
--
Sincerity is everything -- fake that, and you've got it made.
-- George Burns
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function pointer and pointer to function prashant.khade1623@gmail.com C Programming 3 04-11-2008 07:30 AM
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments