Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to consolidate all my JSON lists into one and modify my scripts so that they were more generic and reusable. So far so good.
The problem is that my JSON lists used variables for many pieces of code that performed multiple iterations to create several different but similar navigation and other buttons. So I created global vars that matched those in the JSON list, and my scripts would then redefine those vars according to what was the code was creating. Sounds logical.
It doesn't work. It's as if my scripts are not actually updating the global vars.
So the basic question is, how do I update vars in the JSON list (contained in a single .js file) from other scripts contained in their own .js files?
I am going to keep playing with this, but if anyone can help me out here, I would greatly appreciate it. Here is a quick visual example of what I am attempting to do.
[code=javascript]
// Example JSON list:
var jList =
{
'pageName' : ['pageOne', 'pageTwo', 'pageThree'],
'navigation' :
{
'dom' : 'img',
'parent' : 'body',
'id' : elementId,
'alt' : [altName, alreadyHere],
'title' : [altName, alreadyHere],
'src' : [pathDefault, pathSelected],
'onmouseover': [mouseOver, null],
'onmouseout' : [mouseOut, null],
'onclick' : [loadPage, navAlert],
}
}
[/code]
So as you can see, the only explicit values are in 'pageName' and in 'navigation', 'dom' and 'parent'. The rest are all vars or null. If this list were contained within a script that changed the vars, no problem. However, if the script instead modifies the vars of a JSON list not part of the script, then it doesn't seem to work.
[code=javascript]
/*
* This snippet of code iterates through jList.pageName and sets the vars in
* 'navigation'. Later code sends the updated 'navigation' lists to another
* function for creating each page's nav buttons.
*/
...
for (var index in jList.pageName)
{
var imageId = "nav" + index;
var divNavExist = document.getEle mentById(imageI d);
var pathSelected = "../images/selected" + index.capitaliz e() + ".jpg";
var alreadyHere = 'You are already here.';
var navAlert = new Function("alert ('You are already here.')");
var altName = "Jump to " + index.capitaliz e();
var pathHover = "../images/hover" + index.capitaliz e() + ".jpg";
var pathDefault = "../images/default" + index.capitaliz e() + ".jpg";
var mouseOver = new Function("mouse Hover('" + pathHover + "', '" + imageId + "')");
var mouseOut = new Function("mouse Hover('" + pathDefault + "', '" + imageId + "')");
var loadPage = new Function("logVe rify(" + jList.pageName[index] + ")");
};
...
[/code]
When jList.pageName and jList.navigatio n were internal to this snippit of code, it all worked as designed. Now, it no longer updates the vars in the JSON list.
Thanks ahead of time for your help!
					The problem is that my JSON lists used variables for many pieces of code that performed multiple iterations to create several different but similar navigation and other buttons. So I created global vars that matched those in the JSON list, and my scripts would then redefine those vars according to what was the code was creating. Sounds logical.
It doesn't work. It's as if my scripts are not actually updating the global vars.
So the basic question is, how do I update vars in the JSON list (contained in a single .js file) from other scripts contained in their own .js files?
I am going to keep playing with this, but if anyone can help me out here, I would greatly appreciate it. Here is a quick visual example of what I am attempting to do.
[code=javascript]
// Example JSON list:
var jList =
{
'pageName' : ['pageOne', 'pageTwo', 'pageThree'],
'navigation' :
{
'dom' : 'img',
'parent' : 'body',
'id' : elementId,
'alt' : [altName, alreadyHere],
'title' : [altName, alreadyHere],
'src' : [pathDefault, pathSelected],
'onmouseover': [mouseOver, null],
'onmouseout' : [mouseOut, null],
'onclick' : [loadPage, navAlert],
}
}
[/code]
So as you can see, the only explicit values are in 'pageName' and in 'navigation', 'dom' and 'parent'. The rest are all vars or null. If this list were contained within a script that changed the vars, no problem. However, if the script instead modifies the vars of a JSON list not part of the script, then it doesn't seem to work.
[code=javascript]
/*
* This snippet of code iterates through jList.pageName and sets the vars in
* 'navigation'. Later code sends the updated 'navigation' lists to another
* function for creating each page's nav buttons.
*/
...
for (var index in jList.pageName)
{
var imageId = "nav" + index;
var divNavExist = document.getEle mentById(imageI d);
var pathSelected = "../images/selected" + index.capitaliz e() + ".jpg";
var alreadyHere = 'You are already here.';
var navAlert = new Function("alert ('You are already here.')");
var altName = "Jump to " + index.capitaliz e();
var pathHover = "../images/hover" + index.capitaliz e() + ".jpg";
var pathDefault = "../images/default" + index.capitaliz e() + ".jpg";
var mouseOver = new Function("mouse Hover('" + pathHover + "', '" + imageId + "')");
var mouseOut = new Function("mouse Hover('" + pathDefault + "', '" + imageId + "')");
var loadPage = new Function("logVe rify(" + jList.pageName[index] + ")");
};
...
[/code]
When jList.pageName and jList.navigatio n were internal to this snippit of code, it all worked as designed. Now, it no longer updates the vars in the JSON list.
Thanks ahead of time for your help!
Comment