"Andrew V. Romero" <rrstudio2@icqmail.com> writes:
[color=blue]
> 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">[/color]
<script type="text/javascript">
In HTML 4, the language attribute is deprecated, and the type
attribute is mandatory
[color=blue]
> variablesInUrl = variableArray.length-1[/color]
bad naming, since there are "variableArray.length" variables in the URL,
not one less.
....[color=blue]
> 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.[/color]
Don't do that. Creating global variables gives too big a chance of
overwriting something important (e.g., if one of the variables
was called "window" or "document", then you would be in trouble).
[color=blue]
> 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[/color]
"var" declares a local variable if used inside a function. It declares
a global variable if used outside of a function.
[color=blue]
> 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?[/color]
You don't write "var" in front.
If you just write:
foo = 42;
and "foo" is not declared as a local variable, then it will be created
as a global variable.
You can also create global variables as properties of the global object.
The global variable "window" is a reference to the global object, so writing
window["foo"] = 42;
will also create a global variable called "foo".
Still, I suggest that you create just one global variable and use
the to store the rest:
---
var variables={}; // global variable referencing empty object
function loadURLVariables() {
var searchPairs = location.search.substring(1).split("&");
for (var i in searchPairs) {
var pair = searchPairs[i].split("=");
variables[pair[0]]=unescape(pair[1].replace(/\+/g," "));
/* if this is the result of a form action */
}
}
---
Then you can run "loadURLVariables()" and afterwards, you can refer to the
variables as
variables.foo
or
variables["foo"]
without clobbering the global scope with too many variables.
[color=blue]
> 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?[/color]
Each statement ends in a semicolon, but in some cases the semicolon can
be omitted and is then automatically inserted by the Javascript parser.
That is called "semicolon insertion". In other cases, omitting the
semicolon gives an error.
In practice, it is much simpler *and safer* not to worry about it, and
*always* end ones statements with a semicolon, and not let statements
spanning more than one line have lines ending where a semicolon would
make sense.
/L
--
Lasse Reichstein Nielsen -
lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'