473,549 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically building objects

Hi all. I'm trying to dynamically build menus and objects after my page
loads. Data is stored in an XML file and is parsed at runtime into
Javascript objects. At the moment, I'm working on creating menu items
from these objects. The parsing works fine (using Sarissa), and Firefox
builds the menu no problem. IE, however, does not. The functionality of
the menu will be a single onclick event. It seems that Firefox allows
me to set the onclick event handler for something built dynamically,
but IE won't.

Does anyone have any information on this, or a possible alternate
solution? Thanks.

Jul 23 '05 #1
16 1808
si*****@gmail.c om wrote:
Hi all. I'm trying to dynamically build menus and objects
after my page loads. Data is stored in an XML file and is
parsed at runtime into Javascript objects.
That is a bad idea. XML is sub-optimal as a vehicle for transmitting
data to an ECMAScript enabled client. JSON is much better as the
ECMAScript interpreter can parse that directly to JS objects.
At the moment, I'm working on creating menu items from
these objects. The parsing works fine (using Sarissa), and
Firefox builds the menu no problem. IE, however, does not. The
functionality of the menu will be a single onclick event. It
seems that Firefox allows me to set the onclick event handler
for something built dynamically, but IE won't.
IE provides two independent mechanisms for attaching event handlers to
dynamically created DOM elements.
Does anyone have any information on this, or a possible alternate
solution? Thanks.


Stop using the - setAttribute - method to attach event handlers as when
that works it is just an unspecified side effect. Attach event handlers
by assigning function references to the event handling properties of the
DOM elements, or use a branching W3C - addEventListene r -/ IE -
attachEvent - approach.

Richard.
Jul 23 '05 #2
Interesting. I hadn't thought of using addEventListene r. However, I
have now tried it, and am using the "click" event for these menu items,
but it does not work. It fires off the event immediately (the function
gets called as the page loads), which it shouldn't do, and then
clicking on the item does nothing. Is there some trick to using
addEventListene r? Haven't tried attachEvent yet for IE, and won't until
I get it working in Firefox first.

As for XML and JSON, I'm afraid I have to use XML for this particular
project. It's a contract with another software company, and they want
to use XML.

Thanks.

Jul 23 '05 #3
Richard Cornford wrote:
That is a bad idea.
That is a ridiculously unjustified conclusion.
XML is sub-optimal as a vehicle for transmitting
data to an ECMAScript enabled client.
Perhaps if you define sub-optimal by only considering the size of the data
delivered.

Other factors which may contribute to picking the "optimal" solution
include:
1) Ease of interacting with an existing system
2) Ease of interacting with other developers/companies
3) Use of company-approved technologies or components
4) The requirement to validate the structure of the data using a DTD

Most developers are probably familiar with XML and the tools used to
manipulate it.
Most developers have probably never heard of JSON (non of my co-workers had
when I suggested it recently).

Therefore, saving an extra few kb of data transmission by using JSON might
not be worth it when you consider all the other real-world factors.
JSON is much better as the
ECMAScript interpreter can parse that directly to JS objects.


Which may not be the desired functionality, in which case you need to write
code to parse the objects.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #4
si*****@gmail.c om wrote:
Interesting. I hadn't thought of using addEventListene r.
However, I have now tried it, and am using the "click"
event for these menu items, but it does not work. It
fires off the event immediately (the function gets
called as the page loads), which it shouldn't do, and
then clicking on the item does nothing. Is there some
trick to using addEventListene r?
You are using a CallExpression for the assignment where you should be
using a MemberExpressio n.

(
CallExpression production (ECMA 262 3rd edition; section 11.2.3):-

CallExpression : MemberExpressio n Arguments

MemberExpressio n production (ECMA 262 3rd edition; section 11.2):-

MemberExpressio n:
PrimaryExpressi on
FunctionExpress ion
MemberExpressio n [ Expression ]
MemberExpressio n . Identifier
new MemberExpressio n Arguments
)
Haven't tried attachEvent yet for IE, and won't
until I get it working in Firefox first.

As for XML and JSON, I'm afraid I have to use XML for this
particular project. It's a contract with another software
company, and they want to use XML.


A pity you couldn't involve anyone with pertinent technical skills in
the negotiation of the contract. That is a very risky approach to
contracts because you may find yourself contracted to do the impossible,
and so unable to deliver.

Richard.
Jul 23 '05 #5
Well, I've fixed the problem I was just having with addEventListene r
(that being that I was attempting, foolishly, to test it out using
"alert" and it wasn't working because I had to write a separate
function that is called when the event is fired).

Unfortunately, now the problem I've come across is that when the event
is fired, it is passed exactly one argument, which is an object (do you
perchance know what that object is and what's in it?). I need to know
the id of the menu item that is clicked, so I tried to use this.id in
the event function. This works fine in Firefox, but in IE, this.id is
apparently undefined. What is some way to get around that?

And I've read into JSON a bit, and it doesn't seem that it's perfect
for every scenario. We get the data out of a database (that we the
developers know nothing about) in the form of an XML file that is
validated against a well-formed XML Schema definition. JSON doesn't do
that, nor can the database in question easily output data in JSON
format. The parsing is either on the server or the client, and we don't
get to know anything about the server in this case. So XML it is.

Thanks,
SEAN

Jul 23 '05 #6
Matt Kruse wrote:
Richard Cornford wrote:
That is a bad idea.
That is a ridiculously unjustified conclusion.
XML is sub-optimal as a vehicle for transmitting
data to an ECMAScript enabled client.


Perhaps if you define sub-optimal by only considering
the size of the data delivered.


Who is only considering the size of the download? The processing of that
XML on the client into a form that can be employed is a significant
factor. With JSON you are 100% guaranteed to have a native-code
client-side parser available whenever you are going to be in a position
to do something with the data.
Other factors which may contribute to picking the
"optimal" solution include:
Is this you usual marketing-speak employment of the term 'solution' or
its normal English meaning? (it is difficult to tell with you)
1) Ease of interacting with an existing system
2) Ease of interacting with other developers/companies
XML sent to a client has to satisfy same origin policy so the output
format for the client is not limited by data interchange formats. The
only exception that I can think of pre-existing web services using SOAP,
as a reason for preferring XML.
3) Use of company-approved technologies or components
Only significant if the person doing the approving doesn't have the
sense to see through the buzzwords and understand why they are choosing
technologies.
4) The requirement to validate the structure of the data using a DTD

And how often are you going to be doing that on the client? And if you
were you would be better off validating against a Schema as that avoids
having yet another client-side parser to interpret the DTD.
Most developers are probably familiar with XML and the
tools used to manipulate it.
Such as writing an XSLT to output JSON from it.
Most developers have probably never heard of JSON
(non of my co-workers had when I suggested it recently).
Server-side programmers in other languages have no reason to know about
JSON. That doesn't mean they cannot be persuaded that it is the best
format for sending date to client-side ECMAScript. If they understand
XML they are not going to have much trouble understanding comma
separated name value pairs wrapped in braces.
Therefore, saving an extra few kb of data transmission
by using JSON might not be worth it when you consider
all the other real-world factors.


You are only considering the size of the download. Once downloaded it is
still necessary to process the data into a useable form. The tools for
that with JSON are native to all ECMAScript interpreters, for XML the
browser may or may not provide some (and if it does they will likely
differ between browsers) and if it does not you have to download and
execute an XML parser implemented in client-side code.
JSON is much better as the ECMAScript
interpreter can parse that directly to JS objects.


Which may not be the desired functionality, in which case
you need to write code to parse the objects.


What are you talking about? If the objects are not already in the
desired form (and with JSON they can be in exactly the required form,
which is unlikely to ever be true of an XLM DOM) all you have to do is
read the pertinent data from the JS objects. That is no worse than
extracting the data from XML nodes, and potentially much better.

Richard.
Jul 23 '05 #7
si*****@gmail.c om wrote:
Well, I've fixed the problem I was just having with
addEventListene r (that being that I was attempting,
foolishly, to test it out using "alert" and it wasn't
working because I had to write a separate
function that is called when the event is fired).
At some point it might occur to you that if you post some code people
might have some idea of what you are talking about, and you might get
responses a little more specific to the situation.
Unfortunately, now the problem I've come across is that
when the event is fired, it is passed exactly one argument,
which is an object (do you perchance know what that object
is and what's in it?).
It is the event object.
I need to know the id of the menu item that is clicked,
so I tried to use this.id in the event function.
Please don't tell us that you are going to use the ID with
getElementById to look up a reference to the Element.
This works fine in Firefox, but in IE,
this.id is apparently undefined. <snip>

In what sense do you mean 'in IE'? IE doesn't support -
addEventListene r - at all.
What is some way to get around that?


Almost certainly.

I bet the best solution will be my first suggestion of assigning a
function reference to the onclick property of the dynamically created
DOM element. That gives (near [1]) identical behaviour on the two
browsers you appear to be interested in (and many others) .

Richard.

[1] The difference being that Mozilla passes the event object to the
event handler, while IE makes it available as a global variable.
Jul 23 '05 #8
Maybe you should have read the rest of the thread before you got pissy.
The whole issue was that your suggestion of using the DOM onclick
property *doesn't work*. So I was looking for alternate ways to do it.

Here's some code:
....
var a = document.create Element("A");
a.setAttribute( "onclick", "alert(1);" );
....

That works fine in Mozilla, but not in IE. The function is never
called. It was suggested that I try using addEventListene r (attachEvent
in IE, look up a few posts, that's what I "meant" by "in IE").

That explanation should catch you up. Too bad I had to waste my time
explaining that when you could have read it. If you want to tell me the
same thing, point me to where on this page it says a viable alternate
solution to this problem. Please. Do it. No?

Fine, then I'll tell you why I need the ID. It's not to use
getElementById to look up a reference, because that not only is
completely unnecessary, but also pretty dumb. Part of the ID is a
number that I need to extract and use. It's of the form "button_XXX "
where XXX is some number. I simply extract the XXX out and have the
number I need. "In IE" it doesn't work ("in IE" meaning "using Internet
Explorer as my web browser"), because this.id has no value in the
function called by addEventListene r/attachEvent. Mozilla handles it
fine.

And drop the JSON vs XML crap, it's not relevant.

Jul 23 '05 #9
si*****@gmail.c om wrote:
Maybe you should have read the rest of the thread before
you got pissy. The whole issue was that your suggestion
of using the DOM onclick property *doesn't work*.
Yes it does. It is the most widely supported and reliable approach
available.
So I was looking for alternate ways
to do it.

Here's some code:
...
var a = document.create Element("A");
a.setAttribute( "onclick", "alert(1);" );
... <snip> Too bad I had to waste my time explaining that when
you could have read it. If you want to tell me the
same thing, point me to where on this page
This page?
it says a viable
alternate solution to this problem.
Perhaps you should go back and re-read the thread.
Please. Do it. No?

<snip>

Make me. ;-)

Richard.
Jul 23 '05 #10

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

Similar topics

39
6495
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know...
1
1496
by: | last post by:
In Actionscript 2.0 there is the concept of dynamically generating and naming objects at runtime. Dynamically named objects can be referenced and manipulated programmatically using array-access notation. I'm curious if a similar concept exists in Csharp. I'm building content management apparatus for ASP.NET pages. I was thinking it would...
4
4124
by: brian | last post by:
Hello, I have spen hours trying to find information on dynamically building a table from an array. What I need: I have an array pulling file paths from a directory using a For Next Loop. F is the file. For every instance of F I would like the file path put into the table. The table is just a single column table. I have looked at the...
3
2876
by: Bijoy Naick | last post by:
I have something strange going on.. My .aspx page contains a file upload control, a "Import Data" button, a "newTransactions" <asp:table>, a"SaveTrans" button and an confMsg label. First the user selects a file, then clicks Import Data. The page now reads the PostedFile and displays each line as a new row in the newTransactions table. All...
5
1292
by: Jason | last post by:
Hi all I am building a page dynamically on Page_Load. using Table web controls for this. the web page builds up and loads up and displays ok. but as soon as there is a post back of any sort, all my dynamic building is gone. the page reverts back to it's "original" design time state. i have a OK button, that when the user clicks on this...
7
10050
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the main menu instead of showing all possible options and only enabling/disabling certain ones. I have a table that stores the menu item name, parent...
5
2232
by: Chris Lieb | last post by:
I am trying to add an event listener to the keyup event for some text inputs that I am creating dynamically. I have no problems getting it to work in Firefox, but IE just ignores them. I have created a few functions to aid in making this process work semi-cross-browser. (I develop in FF, but everyone who uses it will be using IE6.) ...
2
1386
by: Dwight Johnson | last post by:
I am building a website in VS2005. I basically have one page, default.aspx, which contains placeholder objects into which I add various controls that are dynamically created. I have a dropdownlist that is created in the InitializeComponent event so I can do a FindControl on it later to determine its new value when it is selected. Based on...
8
2802
by: BillE | last post by:
When I create a control dynamically and it grows according to the content, the control Height property still returns the original default control height instead of the height after expanding. How can I get the height of the control (in code) after it grows? I am building a windows form dynamically, creating controls and placing them on...
0
7548
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7743
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. ...
0
7986
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...
1
7504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7832
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...
0
6074
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...
1
5391
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...
0
3518
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...
0
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.