Writing Friendly Code 2.Think Global
|home / web / dev / friendly|
Writing Friendly Code
2. Think Global
Like a lot of code writers, I put all the global variables up top. Good naming practice is probably the single most important trait to develop when you write code for the Internet (or anywhere else, for that matter). I'll have more to say about naming later, but first let's run through the cast of characters in this script :
The browser id. This one has been around my code since there were two browsers. I start it off as an empty string, and unless my sniffer detects Navigator 4 or IE 4(or above) it stays empty.
Mac is a Boolean switch to indicate the Macintosh platform - it becomes true if a Mac is running IE or Navigator. It's part of the same sniffer that checks the browser. IEÂ behaves differently on a Mac than on Windows; often it has to be screened from things (like text Ranges and outerHTML replacements) that you can use safely on a Win machine. There's none of that here, but when you position content, you have to take things like user fonts into account. The default fonts are considerably smaller on a Mac than on Windows in either browser, and that can impact the size of a container.
This guy is sometimes a Boolean, true or false, but here it is a counter. Navigator loads layers like they are separate documents, and it is not always finished the first time we check. I start things off in the function isReddy() by looking for a string at the end of the document, and I increment this counter a coupleÂ times to give it a chance to get it right. Not to pick on Navigator - IE also usually needs a pause before it's ready to go.
Resize counter. This actually is a toggle: it goes from 0 to 1 and cancels the Event capture onresize handler in Navigator. If you don't cancel it, it's like the pink bunny with the drum, every pixel seems to count as a resize, and it reloads and reloads and reloads... IE doesn't wait for a mouse up to call an onresize either, which seems sort of stupid. At least you don't need to reload IE to redraw the screen.Â This time I would like to pick on Navigator. The more exact and intricate your positioning scheme, the worse it will look when you resize Navigator. Till you reload.
I usually have a newwin, which references new windows opened by the script. Before CSS positioning I put info-tips in new windows. The windows that get created here are for another purpose, as you will see when we get to the function that calls them: nwWind(). newwin makes sure there is at most one script-created window open. The onunload handler, reStat(), closes any window that was left open on the way off the site.Â
Status bar message. I admit it, I like to write to the status bar. This guy does double duty though, as it's part of the load test. If the document.title is not null, I give it to msgStat1 for the defaultStatusÂ property of the window.Â I write to the status bar a lot when I'm testing and don't want to have to mess with alerts. I used it so much when I was figuring out how Navigator loads layers I started thinking that maybe my title really was "undefined."
var sizH=410; var sizW=630;
Default proportions for the display height and width. Nothing sacred here, just what works for this project. I use them as a kind of toggle. If theÂ window is within so much of this in pixels, we don't change anything. Otherwise, we rearrange some of it for the screen. A PDA would have to scroll a lot, so below a certain size you may get an advisory to switch to a text version.Â
I use these values in a couple different places, but they probably wouldn't be globals if Microsoft and Navigator weren't so contrary about the way they calculate document sizes. I'm actually pretty stingy with what I put in my scripts. I generally strip off the comments from working code, and leave an address for the commented page.
Comments are welcome
Created: Dec. 2, 1999
Revised: Dec. 7, 1999