How to make javascript variable global

14,034

Any variable can be "made global" by attaching it as a property of the window.

window.data = data;

You can now access data as a global variable.

Share:
14,034
user123_456
Author by

user123_456

Updated on July 27, 2022

Comments

  • user123_456
    user123_456 almost 2 years

    I need to make this data variable global:

    $.ajax({
        url: "get_data.php",
        cache: false,
        dataType: 'json',
        data: {},
        success: function(data) {
            for(var i = 0; i < data.results.length; i++) {
                if(my_data.hasOwnProperty(data.results[i].id)) {
                    my_data[data.results[i].id].name = data.results[i].name;
                }
            }
        });
    

    I want to have this globally declared. Do I need to declare it as array?