PDA

View Full Version : global variables


looka
09-30-2004, 07:55 PM
how can i create a global variable in a function?

i would like the string in 'in' to become global variable:

function sth (in) {
eval(in + ' = some_value;');
}

liorean
09-30-2004, 08:15 PM
Well, first of all, in is a reserved keyword in JavaScript and is an operator. It may not be used in identifiers.

Second drop the eval and instead make an assignment to window[stringcontainingthevariablename], which makes it a global variable.

function sth(str){
window[str]=some_value;
}

looka
09-30-2004, 08:51 PM
thank u! :)