I'm trying to pass an object out of a function. Here's my code:
<script type="text/javascript">
// finds the head element; creates a script with passed url; appends it to the head
function loadJSON(url) {
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript'; // isn't this 'application/json' really?
newScript.src = url;
headID.appendChild(newScript);
}
function parseJSON(o) {
if (content == null) {
document.write("<p>parseJSON(): JSON failed.</p>");
return false;
}
isDST = "<strong>not</strong> ";
if ( o.rawOffset == o.dstOffset ) {
isDST = "";
}
document.getElementById("geonames").innerHTML = "<p>The time now is " + o.time + " in " + o.countryName +".</p>";
}
var lat = "47.01"; var long = "10.2"; // test data for austria
var jsonUrl = " {{URL}} ";
loadJSON(jsonUrl);
</script>
What I want to do is instead of using the object o inside the parseJSON function I want to pass the object out so that after the loadJSON() call I can do things with the object. I find it's not "neat" having the innerHTML calls inside the parse function.
I've tried lots of iterations of, eg declaring "var jsonOutput" at start of script and then making "jsonOutput = o" but that just leaves jsonOutput undefined. If I try to return jsonOutput then I don't know where it's being returned to, I can't use it anyway.
Do I need to make a global variable? I've tried a simple "global jsonOutput" declaration but it doesn't seem to be modified inside the function.
Am stuck, please help.