473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

eliminating a global variable

I've created a site where the pages generally contain a table of
contents (site map) down the left side. When each page loads, the
first function is called. The second function populates a global var
(array) of all the links.

I try to avoid using global variables, but I am stumped how to
eliminate this one (TocLinks) because other functions need to iterate
through it. I think the answer is to create a custom object that the
other functions can access. But I need some pointers on how to
proceed.

Here's the function that the pages call initially and the 2nd function
that it calls:

function SetCurrPgLink()
{
var parElem;
var parElemUL;
var findUL;

GetPageLinks();

for (var i = 0; i < TocLinks.length; i++)
{
var x = TocLinks[i].href;
if (x.indexOf(CurrentPage) > -1)
{
CurrentPgIndex = i;
TocLinks[i].id = 'CurrentPgA';
parElem = TocLinks[i].parentNode;
parElem.style.backgroundColor = 'white';
findUL = parElem.parentNode.tagName.toLowerCase();
if (findUL == 'ul')
{
parElemUL = parElem.parentNode;
parElemUL.style.display = 'block';
}
parElemUL.parentNode.className = 'FolderOpenLI';
break;
}
}
}

/* fine-tune list of anchors */
function GetPageLinks()
{
var CountPgItems = 0;
var TocDivNode = document.getElementById('TocDiv');
var TocLinksAll = TocDivNode.getElementsByTagName('a');

for (var n = 0; n < TocLinksAll.length; n++)
{
if (TocLinksAll[n].parentNode.className == 'PageItem')
{
TocLinks[CountPgItems] = TocLinksAll[n];
CountPgItems++;
}
}
}
Jul 23 '05 #1
1 1121
On 17 Nov 2004 10:59:56 -0800, Jeff Gutsell <je*********@fuse.net> wrote:

[snip]
I try to avoid using global variables, but I am stumped how to eliminate
this one (TocLinks) because other functions need to iterate through it.
I think the answer is to create a custom object that the other functions
can access. But I need some pointers on how to proceed.
Nothing as complicated as that. Just return the array.

function getPageLinks() {
/* Using the document.links collection is potentially simpler
* but it's based on the assumption that checking for PageItem
* in the class attribute is sufficient to obtain the correct
* links. If not, restore your original code.
*/
var links = document.links, temp = [];
for(var i = 0, n = links.length, c; i < n; ++i) {
c = links[i];
/* It may be more appropriate to use indexOf, rather than using
* a normal comparison as the class attribute can contain more
* than one class name as a space-separated list.
*/
if(c.parentNode.className == 'PageItem') {
temp[temp.length] = c;
}
}
return temp;
}
var tocLinks = getPageLinks();

[snip]
findUL = parElem.parentNode.tagName.toLowerCase();
if (findUL == 'ul')


The element name is always uppercase for HTML documents, and
case-sensitive for XML-based documents. Assuming this is just HTML, it
would be quicker to use

if(parElem.parentNode.tagName == 'UL')

and omit findUL entirely.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

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

Similar topics

8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
27
by: Mr. Ed | last post by:
This should be easy, but I can't immediately see what the answer is... I have some code which needs to be re-entrant. It also needs to create a unique identifier. The obvious way to create a...
28
by: Mr. Ed | last post by:
This should be easy, but I can't immediately see what the answer is... I have some code which needs to be re-entrant. It also needs to create a unique identifier. The obvious way to create a...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.