OK I want to create a menu from an XML file. I am parsing it with JS.
Now it seems to be working fine until it comes time to build the menu.
I am building arrays in JS and the arrays are inside a function. How
can I make these arrays been seen by other functions? Or cant I?
here is an abridged snippet:
var xmlDoc;
if (document.imple mentation &&
document.implem entation.create Document){
alert ("MOZ");
xmlDoc = document.implem entation.create Document("", "", null);
xmlDoc.onload = readXML;
}
else if (window.ActiveX Object){
alert ("IE");
xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
xmlDoc.onreadys tatechange = function () {
if (xmlDoc.readySt ate == 4) readXML()
};
}
else{
alert('Your browser cannot handle this script');
//return;
}
xmlDoc.load("ht tp://brewdente.homed ns.org/yellowstone/js/menu.xml");
function readXML(){
.....
eval(parentmenu + '= new Array(' + '"' + labelTxt + '"' + ',' +
'"' + linkTxt + '"' + ',"",' + childrenTxt + ',' + heightTxt + ',' +
widthTxt + ');');
//will look like this
//Menu1=new Array("somethin g","http://","",0,20,2 25);
} //end readXML
then the rest of the functions are down below. I 'alert' all values
and everything is ok and I even am able to access different elements of
my array inside this function. How can get to these arrays outside of
this function? My code then says 'Menu1' not defined and I believe
scope is the problem. Any ideas? 4 1556
jimmygoogle wrote: OK I want to create a menu from an XML file. I am parsing it with JS. Now it seems to be working fine until it comes time to build the menu. I am building arrays in JS and the arrays are inside a function. How can I make these arrays been seen by other functions? Or cant I?
here is an abridged snippet:
[...]
function readXML(){
.....
eval(parentmenu + '= new Array(' + '"' + labelTxt + '"' + ',' +
Why eval?
Presuming you have parsed the XML file and put values into the variables
somewhere above, then why not add them to a global object declared
elsewhere (say where you declared xmlDoc):
MenuData[parentmenu] = [labelTxt, linkTxt, childrenTxt,
heightTxt, widthTxt];
Or try something like this:
var MenuData = {};
function parseXML()
{
var pNodes = oLoadedXML.getE lementsByTagNam e("menuitem") ,
pNode, cNode, cNodes,
i, j, m, n;
for (i=0, m=pNodes.length ; i<m; ++i){
cNodes = pNodes[i].childNodes;
MenuData['menuitem_' + i] = {};
pNode = MenuData['menuitem_' + i];
for (j=0, n=cNodes.length ; j<n; ++j){
cNode = cNodes[j];
if (cNode.tagName) {
pNode[cNode.tagName] = (cNode.firstChi ld
&& cNode.firstChil d.nodeValue) || '';
}
}
}
// Debug - show the contents of MenuData
var tmp0, txt = '';
for (var prop in MenuData){
txt += prop + ':\n';
tmp0 = MenuData[prop];
for (var prop0 in tmp0){
txt += prop0 + ': ' + tmp0[prop0] + '\n';
}
}
alert(txt);
// End Debug
}
MenuData contains the data loaded from the XML file and is available
globally, use for..in to get at it. The MenuData object could be
created more generically, over to you.
Have you considered creating DOM elements as you parse the XML and add
them directly to the document? All the stuff in the XML file will then
exist in the DOM which is presumably what the functions 'down below' are
about to do.
You could put your data into a JSON structure, then eval that to get
your object (light on security if cross-domain, read up on the issues) -
check out <URL:http://www.crockford.c om/JSON/index.html>
You could also add methods to the MenuData object to get and set
internal properties to hold the data.
[...]
--
Rob
RobG wrote:
[...] You could put your data into a JSON structure, then eval that to get your object (light on security if cross-domain, read up on the issues) - check out <URL:http://www.crockford.c om/JSON/index.html>
Haven't seen any simple JSON examples, so here's one (I hope...):
[ menu_jsontxt.xm l ]
<?xml version='1.0'?>
<jsontext>
{ "menuitem_1 " : {
"name" : "1",
"node" : 1,
"parent" : 0,
"label" : "stuff",
"link" : "/about/hobbies/computing.html" ,
"children" : 0,
"subchildre n": 0,
"height" : 20,
"width" : 100
},
"menuitem_2 " : {
"name" : "2",
"node" : 2,
"parent" : 0,
"label" : "Computing" ,
"link" : "/about/hobbies/computing.html" ,
"children" : 0,
"subchildre n": 0,
"height" : 20,
"width" : 100
},
"menuitem_3 " : {
"name" : 3,
"node" : 3,
"parent" : 0,
"label" : "things",
"link" : "/about/hobbies/computing.html" ,
"children" : 0,
"subchildre n": 0,
"height" : 20,
"width" : 100
}
}
</jsontext>
[ menu_json.html ]
<script type="text/javascript">
var oLoadedXML;
function loadXML(sImport XML)
{
if( document.implem entation &&
document.implem entation.create Document ) {
oLoadedXML = document.implem entation.create Document("","", null);
oLoadedXML.asyn c=false;
var loaded = oLoadedXML.load (sImportXML);
if (loaded) {
parseXML();
}
} else if( window.ActiveXO bject && /Win/.test(navigator .userAgent) ) {
oLoadedXML = new ActiveXObject(" Msxml.DOMDocume nt");
oLoadedXML.asyn c = false;
oLoadedXML.onre adystatechange = function () {
if (oLoadedXML.rea dyState == 4) parseXML();
}
oLoadedXML.load (sImportXML);
} else {
alert("I'm not clever enough to make this script "
+ "work in your browser.");
return;
}
}
var MenuData = {};
function parseXML()
{
var jsonText = oLoadedXML.getE lementsByTagNam e('jsontext');
if ( !jsonText.lengt h ) { return; }
MenuData = eval('(' + jsonText[0].firstChild.nod eValue + ')';
// Just to show the contents of MenuData
var tmp0, txt = '';
for (var prop in MenuData){
txt += prop + ':\n';
tmp0 = MenuData[prop];
for (var prop0 in tmp0){
txt += prop0 + ': ' + tmp0[prop0] + '\n';
}
}
alert(txt);
// End 'just to show'
}
loadXML("menu_j sontxt.xml")
</script>
--
Rob
thanks for input - am i basically making an array of arrays? now if i
want access this array outside the parseXML function how would i? i
need to have an array defined which is accessable by other functions
that reads like
Menu1=new Array("menu1"," http://","",0,20,2 25);
Menu2=new Array("menu2"," http://","",0,20,2 25);
.......
loadXML("menu_j sontxt.xml")
alert( -how would i acces it -)
</script>
jimmygoogle wrote: thanks for input - am i basically making an array of arrays? now if i
The code I posted creates an object that contains one object for each
menuitem (objects can contain anything, including other objects). I
think you are better to put all the data into one object, rather than
having some unknown number of global variables floating around.
The structure of the object is exactly the same as the menu_jsontxt.xm l
file in my second post.
want access this array outside the parseXML function how would i? i
MenuData is a global variable, so you can access it from anywhere once
you create it. It would probably be better for it not to be a global
and for references to be passed around - but without seeing the rest of
your application, I can't say for sure.
need to have an array defined which is accessable by other functions that reads like
Menu1=new Array("menu1"," http://","",0,20,2 25); Menu2=new Array("menu2"," http://","",0,20,2 25);
If you load the script I posted and use your menu.xml file, it will show
an alert with the structure of the object.
You can get the data back out using a for..in loop, like the debug part
of my script that displays the contents of the object.
You can get the data explicitly using dot notation:
MenuData.menuit em_1.label is 'stuff'
MenuData.menuit em_1.link is '/about/hobbies/computing.html'
and so on.
...... loadXML("menu_j sontxt.xml") alert( -how would i acces it -)
alert(MenuData. menuitem_1.labe l); // shows 'stuff'
[...]
--
Rob This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michelle Keys |
last post by:
I am trying to call a print function to print a string from a database
using javascript.
Which is RC_DATA of Varchar2(2500).
This is a javascript is not being used. I have a thing that needs to be
modified:
if(e.CommandName =="Print")
{
string parsedreceipt = null;
parsedreceipt = DecodeReceipt (e.Item.Cells.Text);
Session = parsedreceipt;
|
by: gg |
last post by:
I'm currently trying to strengthen up the security on a large ASP.NET
application (a web content management system). The primary objective is to
prevent people from evesdropping for passwords and other sensitive
information, with a secondary objective of preventing Harry the Hacker from
having his (her) evil way.
Logging on is secured by never storing the passwords in plain text and using
a combination of MD5 hashes for the...
|
by: John Boy |
last post by:
Hi,
Can anyone help. This is really doing my nut in. 3 years ASP exp. and
now doing .DOT which is a step in the wrong direction.
Basically I am left with the code of a guy who has left. When I click a
button on a pop-up window the javascript for that button click does a
'button.form.submit'. On the Server side there is a Button click event
for this button, but for some reason it no longer fires. It worked fine
before and everything...
|
by: Woody Splawn |
last post by:
I am finding that time after time I have instances where I need to access
information in a variable that is public. At the same time, the books I
read say that one should not use public variables too much - that it's bad
programming practice. Is there an easy way to deal with this? I would like
to do things in the "Best Practices" way but at the same time I don't want
to make a federal case out of it.
This comes up over and over...
|
by: thomasp |
last post by:
Variables that I would like to make available to all forms and modules in my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. What is the normal way to
do this?
Thanks,
Thomas
--
| |
by: David Whitchurch-Bennett |
last post by:
Hi There,
I have created a very simple web user control, containing only a combo box.
I have created a class which contains some private object variables, for
example...
Private _anObject as Object
Public Property anObject() As Object
Get
|
by: jonathan.beckett |
last post by:
I have been working on a client project recently that is using winforms
..NET user controls within web pages in Internet Explorer, and need to
find out how to make the user control communicate back to the webpage
(typically to tell it that it has finished doing something). I started
digging around and found out that I needed to implement a COM interface
for the .NET user control, and pick up the events from that in
Javascript on the...
|
by: pbmods |
last post by:
VARIABLE SCOPE IN JAVASCRIPT
LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN )
PREREQS: VARIABLES
First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
|
by: tshad |
last post by:
Using VS 2003, I am trying to take a class that I created to create new
variable types to handle nulls and track changes to standard variable types.
This is for use with database variables. This tells me if a variable has
changed, give me the original and current value, and whether the current
value and original value is/was null or not.
This one works fine but is recreating the same methods over and over for
each variable type.
...
|
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,...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |