473,778 Members | 1,862 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 2032
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
1375
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
4917
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
2546
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
3212
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
1954
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
4967
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
1349
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
2733
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
35623
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
10292
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
10122
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...
1
7471
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...
0
6722
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5368
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
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
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
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.