Writing Friendly Code 3.More Global! | WebReference

Writing Friendly Code 3.More Global!

home / web / dev / friendly

Writing Friendly Code

3. More Globals

The globals up to now are regular characters in most of my scripts, but now we are getting specific to this application:

var yPage = new Array();

Each document in this series of Web "magazines" has an initially unknown number of pages. Page Array is built during setPg(), the first process after the load check, to count them. I'll explain how it does that when we get there. The n's following are assigned there as well.

var n = 0;

This holds the length of the yPage array - one more than the number of pages, starting with zero for the cover page.

var n1,n2,n3;

These three are for convenience. They are the results of n-1, n-2 and n-3. The magazine has two regular features that are linked to outside source files, a forum and a resource page. The cover page is also a special case, but it is always page0, and has the first button in the navigation bar of page tabs.

I want the features to act like regular pages, but I also want them to stay at the far end of the array instead of the beginning.  This is getting ahead of ourselves, but the major function after setting things up is turnPage(pg), where pg is the page number you want to go to. By assigning the length of the array of pages to n, n-1 is always the last page, and n-2 the one before that. So I can call turnPage(n-2), and always get to the discussion forum. 

Except in setPg() I change its name to n1. If I am so stingy with globals, why'd I shoot the works on these guys? I know, I just sprang it on you, but what's the difference? I'll show you when we get to turnPage().

var pg=1;

This little guy, almost at the end, is the most popular kid in the script. We got everybody checking out pg, which tracks the current selected page. There are quite a few times when you are away from the selected page and need to get back.  The page buttons also need to know where you are, so they can change state as you move through. Then you will know where you are, too. 

var infos=new Array();

This is an array built from the script's own help files and the current document's collection of informative tips. The first five are defined as strings at the end of the script. They get put in this array in our friend setPg(). Besides the help buttons, there are some green underlined links here and there in the magazines. They are like footnotes or references. 

They don't navigate anywhere - they use a JavaScript command in the link's href property to call inform(what). (what) is the  index of the string that gets written to the message. In this whole script, the single hardest thing to do was getting the "close" button in the popup window to work in both browsers. I'd get one working, and the other wouldn't. Fix that one, the first crashed.

Event handlers for positioned layers that don't exist until they are needed -  fine place for the two browsers to hear different drummers. There will be more about this, by and by.


That's all of 'em. Nothing fancy. A few toggles and switches, a couple counters and values, an array of messages and a few trackers. No prototypes, no new Objects at all. We don't need them. And note that most of the variables can be reused in many other types of documents. Cut 'n' paste code. 

I put the message strings at the end of the script out of the way. They could be in an outside file, but I'd rather keep them here. Ease of maintenance. We'll take a look at them as we finish up, but now I want to start on the functions. Aside from these comments, the script as you see it is just as it is in the working script, with one exception. 

All of the procedures in the code are in local function bodies except for the browser sniffer and the window event handlers. I put my window handlers at the very end of the code, so I know that everything has been read before it starts reacting to the environment. For the purposes of this discussion, however, I'm moving them up right after the sniffer. Bear in mind they are actually read last, after all the other functions have been catalogued by the script engine.

I moved them up because except for the sniffer, and some browser specific variable assignments in the infotips section, they are the only live code. Nothing else makes a move until the document loads, and then nothing happens until the success of the load is verified. This load check is always good practice, but it is crucial in code that uses objects that only exist in a document that has not been encountered by the script before.

We'll go through the script the way the script interpreter in a browser does it. At this point we have parsed the document head and loaded the stylesheet. We initialize the variables, add the functions as properties of the global object, and come to the first bit of active code.

Checking ID's 

Comments are welcome

 


Created: Dec. 2, 1999
Revised: Dec. 7, 1999

URL: https://webreference.com/dev/friendly/