Variables globales | Programar Plus

Declarar variable fuera de la función …

var oneVariable;

function setVariable(){
    oneVariable = "Variable set from within a function!";
}

function getVariable(){
    alert(oneVariable); // Outputs "Variable set from within a function!"
}

O … adjúntelo al objeto de la ventana

function setValue() {
    window.myValue = "test";
}

function getValue() {
    alert(window.myValue); // "test" (assuming setValue has run)
}
(Visited 9 times, 1 visits today)