Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  165] [ 3]  / answers: 1 / hits: 16244  / 13 Years ago, wed, august 17, 2011, 12:00:00

I've spent the last two hours trying to figure out how to do this but nothing is working. Here is a short sample of some of my code. I want to get arrtime and several other similar variables out of the function so I can use them globally. Any ideas? Nothing too complicated please, I'm no expert (obviously).



function showTest(str) {
........

var arrayvals = JSON.parse(xmlhttp.responseText);
var arrtime= (arrayvals[0]);
}
var testvar=arrtime;
document.getElementById(testing).innerHTML=testvar;

More From » variables

 Answers
4

The clean way to do this is using js-object notation:



function showTest(str) {
//other code
return {arr: arrayvals, tm: arrtime};
}

var func_result = showTest(blah-blah);
var testvar =func_result.tm;
var testvar2=func_result.arr;


But it's generally a bad idea to have global vars. Why do you need it?



Update sample code with global object



globals = {};
function q(){
globals['a'] = 123;
globals[123] = 'qweqwe';
}
function w(){
alert(globals.a);
//alert(globals.123); //will not work
alert(globals[123]); //that's OK.
}
q();
w();

[#90565] Monday, August 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neildrews

Total Points: 166
Total Questions: 103
Total Answers: 85

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
neildrews questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Tue, Oct 12, 21, 00:00, 3 Years ago
Tue, Mar 23, 21, 00:00, 3 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
;