I'm new to JS, and I don't understand why this doesn't work. The following is in a file called test1.js
var greeting;
function randomGreeting(){
var greet = new Array("BORING JS TEST ALERT", "EXCITING JS TEST ALERT", "AWESOME JS TEST ALERT");
var randGreet = Math.floor(Math.random() * greet.length);
greeting = {
alert: greet[randGreet]
};
}
In a separate test2.js file:
alert(greeting.alert);
In my HTML I have randomGreeting() being called in the body's onLoad, and I have test1.js loaded before test2.js, but I still get undefined from the console when test2 is ran. Why?
randomGreetingcalled?randomGreetingis called in the body'sonLoad.alert(greeting.alert)is not in onload? That means it runs beforerandomGreeting.alert()runs before theonloadevent handler..