473,508 Members | 2,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mozilla doesn't handle function pointer well?

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?
Jul 23 '05 #1
5 2014
On Thu, 30 Sep 2004 11:15:25 -0400, nick <nb**************@hotmail.com>
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.
Jul 23 '05 #2
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" <M.******@blueyonder.co.invalid> wrote in message
news:opse5cajqcx13kvk@atlantis...
On Thu, 30 Sep 2004 11:15:25 -0400, nick <nb**************@hotmail.com>
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.

Jul 23 '05 #3
Lee
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;

Jul 23 '05 #4
On Thu, 30 Sep 2004 11:56:17 -0400, nick <nb**************@hotmail.com>
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" <M.******@blueyonder.co.invalid> wrote in message
news:opse5cajqcx13kvk@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.
Jul 23 '05 #5
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
Jul 23 '05 #6

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

Similar topics

12
1341
by: Bart | last post by:
Hi there, I've got a strange problem. Used to use IE. Now switched to Mozilla (1.4). This works fine in IE but produces nothing (not even an errormessage) in Mozilla:
4
4894
by: Pai | last post by:
hello there, I am trying to rersize the window it works find in IE but doea not work with mozilla window.attachEvent(onload,MWSOnLoad); window.onload = function (MWSOnLoad) { alert('hello');...
4
2520
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
10
3187
by: David | last post by:
Hi everyone, Hoping there are some .js/browser experts out there that can help with this weird problem. I have made a swap div routine and applied the events to menu buttons with a closer...
1
1941
by: mr_burns | last post by:
Hi there, I am using the following function to import a xml file whether the users browser be IE or Mozilla: function importXML(file) { var xmlDoc; var moz = (typeof document.implementation...
1
4938
by: Ryan Stewart | last post by:
If you don't want to read this post because of its length, I understand. I've spent two and a half days on this problem and have a good deal of information to relate. And this is kind of a long...
2
1331
by: mlybarger | last post by:
we currently have a heavy dataisland based site and would like to get it working on mozilla to take advantage of the js debuggers and other various tools. there have been various issues along...
14
2712
by: Howard | last post by:
Hi, I recently had a problem where I decided to store objects in a vector. (Previously, I had always stored pointers in vectors). Well, naturally, when storing an object in a vector, using...
2
35494
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
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...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
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
3204
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
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.