473,839 Members | 1,465 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('javascri pt 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 2037
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('javascri pt 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.******@bluey onder.co.invali d> wrote in message
news:opse5cajqc x13kvk@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('javascr ipt 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.******@bluey onder.co.invali d> wrote in message
news:opse5cajqc x13kvk@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('javascri pt event: u clicked '+v);
return false;
};
It would be both more reasonable and downwards compatible to write

function ocevent(v)
{
alert('javascri pt 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("retur n ocevent(2);");

items["003.jpg"] = new Object();
items["003.jpg"].text = "003";
items["003.jpg"].href = "#"; // TODO!
items["003.jpg"].click = new Function("retur n 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 HTMLAnchorEleme nt object or its equivalent).

However, the recommended standards compliant technique as of the W3C DOM
Level 2 Events Specification is

a.addEventListe ner('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
1380
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
4920
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'); window.resizeTo(810,750); top.outerWidth=810;
4
2549
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 want all of the spaces to be put in the first slots of the array. IE 6 does this. But Firefox 0.9.1 doesn't, and I don't know why. I have not been able to reproduce it in very simple form (which is itself a puzzle). But example code is...
10
3222
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 layer behind the menus. The closer div has a lower index than the submenu divs so it appears behind them. The closer div contains a transparent gif with an event applied to it to close all of the divs when moused over.
1
1959
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 != 'undefined') && (typeof document.implementation.createDocument != 'undefined'); var ie = (typeof window.ActiveXObject != 'undefined');
1
4976
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 shot, but I'm just hoping someone here has experienced a similar problem and has a better idea of what's going on than I do. First, I've tested this in IE 6.0, Netscape 7.2, Mozilla 1.75, and Firefox 1.0. It works fine in IE (even though I was...
2
1350
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 the way. the current issue is with using document.selectNodes( xpathString ). mozilla doesn't seem to define this natively, and i can't seem to find a general workable solution to handle data islands. I found this posting ...
14
2741
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 push_back, the object I had in hand was getting copied (twice, in fact). That led to a problem, in that my object contained a "handle" to another object, and when the object being pushed went out of scope and was destroyed, the referenced object was...
2
35656
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 implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
0
9697
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10586
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10293
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7828
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4484
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 we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3134
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.