A beginner question indeed but can't find anywhere on stackoverflow that actually solves this, so any help would be really appreciated. I just want to define a private variable and then return it to the global scope.
In this example, I can access x in the JS console after refreshing the page - but the script runs before the body loads (source at end of head tag) and I cannot create elements that relate to the body content since they haven't loaded yet:
var x = 5;
window.onload = function() {
var y = 10;
}
I cannot access variable y, presumably because it's a private variable that's not available on the global scope (am I actually correct here?). Using return fails. I just want to be able to create variables from the body, but I can't because the script hasn't loaded in time.
How can this be achieved? I actually can't solve this presumably simple problem. Thanks for any help here.