Create Global Var in Function

Giganews Newsgroups
Subject: Create Global Var in Function
Posted by:  Andrew V. Romero (rrstudi…
Date: Mon, 14 Jul 2003

I have been working on a function which makes it easier for me to pull
variables from the URL.  So far I have:
<script language="JavaScript">

var variablesInUrl;
var vArray = new Array();

function loadUrlVariables()
    varString =;
    //removes ? from varString
    varString = varString.substring(1,varString.length);
    //split into array containing variable=value
    variableArray = varString.split('&');
    variablesInUrl = variableArray.length-1

    for (i=0; i<= variablesInUrl ; i++)
        temp = variableArray[i].split("=");
        vArray[i] = new Array();
        vArray[i][0] = temp[0];
        vArray[i][1] = temp[1];

//function returns variable's value
function getValue(varName)
    for (i=0; i<=variablesInUrl; i++)
        if (vArray[i][0] == varName)
            return vArray[i][1]
    alert ("Variable: "+varName+" was not found.")


//get value of a variable named step
alert( getValue("weight") )


This works but in order to get the value of the variable, I have to use
the getValue function.  What I would really like is for the
loadUrlVariables function to create global variables out of the
variables present in the URL.  This way I could just access them
normally (i.e. alert(weight) would display the value in the variable
weight).  I was reading some and tried a test.  Inside of the
loadUrlVariables I put
var testGlobal = document.body
testGlobal = "Am I a Global Variable"

then after my other alert, I did alert(testGlobal), but I got an
testGlobal is undefined error.  So how do I make a global variable from
inside a function?

On an unrelated note:
    In javascript, after each line are you suppose to put a ;?  I see some
scripts that have a semi colon after each line and some that don't.  I
haven't notice that it makes any different, but am wondering what the
technically right way is to do it?

    Andrew V. Romero