Forum Moderators: open
function DoITonce() {
var a = One() // a math.random function
var b = Two() // a math.random function
var c = Three() // a math.random function
var x = (a + b + c) // =6, 7, 8 or 9
switch(x) {
case 6:some functions();
break
case 7:some more functions();
break
case 8: somefunctions();
break
default: somemore functions();
}
and then
function doITagain() {
var a = One()
var b = Two()
var c = Three()
var y = (a + b + c)
switch(y) {
case 6:some xfunctions();
break
case 7:some more fxunctions();
break
case 8: somexfunctions();
break
default: somemore xfunctions();
}
and then
doITmore() ... using var z = (a + b + c) and switch(z) ...
Any pointers appreciated.
// These are global, not scoped in a particular function
var a = One() // a math.random function
var b = Two() // a math.random function
var c = Three() // a math.random function
function DoITonce() {
var x = (a + b + c) // =6, 7, 8 or 9
...
}
function doITagain() {
var y = (a + b + c)
...
}
But global variables are sloppy and more recent methods instead create a "namespace" to contain all of your variables and methods. That way, instead of having a bunch of global variables and functions (which could potentially conflict with someone else's code if you decide to add a 3rd party script to your page), you only have a single global object, thus reducing the chances for conflicts and keeping your code modularized and easily transportable.
Try Googling for Javascript namespace for more on this method.
Just on the point of conflicting scripts, I posted earlier without a reply, but maybe it's worth another try: is there any obvious reason why these scripts should conflict?—
//change by ID ...Code_Punk
function box (boxname,menustate) {
document.getElementById(boxname).style.visibility = menustate}
// called by box('boxname', 'menustate')
//and change by class ...Shawn Olson
function changecss(theClass,element,value) {
var cssRules;
if (document.all) {
cssRules = 'rules';
}
else if (document.getElementById) {
cssRules = 'cssRules';
}
for (var S = 0; S < document.styleSheets.length; S++) {
for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
document.styleSheets[S][cssRules][R].style[element] = value;
} } } }
//called by, eg changecss('.myClass','color','black')
...both being used within a single switch case.
CT