0

I am trying to set content in my main div based on the variable name. For example, if I run: changeMain(nearbyCows) I want to see 'tehe', instead it seems window[page] is undefined. Any ideas why?

function changeMain(page) {
    var nearbyCows = "<a>tehe</a>";
    var nearbyChickens = "<b>lol</b>";
    var search = "<i>meh</i>";

    $("main").html(window[page]);
}
4
  • nearbyCows is a local variable in this function. It's not in the scope of the caller, unless it has its own variable with the same name. Commented Mar 12, 2014 at 13:10
  • You can only use window[name] to access global variables, not local variables. Commented Mar 12, 2014 at 13:11
  • @A.Wolff The value of page could be one of three, nearbyCows, nearbyChickenso or search (otherwise it would fail to set the html) Commented Mar 12, 2014 at 13:11
  • Why does the question have a down vote? Commented Mar 12, 2014 at 13:53

4 Answers 4

1

You're creating this variables in the scope of the function. The properties of window refer to the global scope. If you want to use the global scope like this, then you need to move your variable declarations out of the function:

var nearbyCows = '<a>tehe</a>';
var nearbyChickens = '<b>lol</b>';
var search = "<i>meh</i>";
function changeMain(page) {
    $("main").html(window[page]);
}

But, then your essentially just using the window as a look up hash, and it would be better and cleaner to keep your own look up object instead of co-opting global.

for example:

var MainChanger = {
    nearbyCows: '<a>tehe</a>',
    nearbyChickens: '<b>lol</b>',
    search: "<i>meh</i>",
    change: function(page) {
        $('#main').html(this[page]);
    }
};

MainChanger.change('nearbyCows');
Sign up to request clarification or add additional context in comments.

Comments

0

var nearbyCows is not defined on global scope but as local variable, use then: nearbyCows = "<a>tehe</a>"; without using directive var.

Comments

0

Use an object, not separate variables:

function changeMain(page) {
    var items = { nearbyCows: "<a>tehe</a>",
                  nearbyChickens: "<b>lol</b>",
                  search: "<i>meh</i>"
    };
    $("#main").html(items[page]);
}

Then you can call it like this:

changeMain('nearbyCows');

Comments

0

You must know that there is multiples type of variable.

You have the global variable which is assign the the window (not a good practice). You can declare it like that :

window.varName = ...;
window['varName'] = ...;
varName = ...;

There is variable scope, that is the usual way to declare variable :

var varName = ...;

Those variables are accessible by inner scope aswell but not accessible by outer scope.

There is also object properties. You create an object and assign value :

var obj = {};
obj.varName = ...;
obj['varName'] = ...;

If you can see, there is no way you can access a scoped variable with a dynamic string, you need object or global variable. :

function changeMain(page) {
    //Global variable, not recommended
    window.nearbyCows = "<a>tehe</a>";
    window.nearbyChickens = "<b>lol</b>";
    window.search = "<i>meh</i>";

    $("main").html(window[page]);
 }

or :

var obj = {} //Declare outside if you want to access it outside the function
function changeMain(page) {
    // var obj = {}; //or inside if you want it to be a private variable
    //Object properties, recommended
    obj.nearbyCows = "<a>tehe</a>";
    obj.nearbyChickens = "<b>lol</b>";
    obj.search = "<i>meh</i>";

    /*Alternative declaration, faster to process :
    obj = {
        nearbyCows : "<a>tehe</a>",
        nearbyChickens : "<b>lol</b>",
        search : "<i>meh</i>"
    }*/

    $("main").html(obj[page]);
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.