views:

324

answers:

2

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.

A: 

I'm not quite sure if I understand your scenario completely, but have you tried to return a whole object as opposed to returning just a boolean or json?

function parseJSON(o)
{
   // Your code here
   var result = new Object();
   result.IsSuccess = true; // or false
   result.JSON = someData; // your json output
   return result;
}

function loadJSON()
{
   // Your code here
   var jsonParseResult = ParseJSON(o);
   if (jsonParseResult.IsSuccess)
   {
       // More code here involving jsonParseResult.JSON
   }
}
DrJokepu
Hmm, the parseJSON is the callback, so it has the JSON code received from offsite as object "o", that's the data I want to expose outside the function. I tried "var jsonOutput = {};" (in and out of the function) and then setting "jsonOutput = o; return jsonOutput" but that doesn't seem to do it.
+1  A: 

Also, it seems that you are trying to load the JSON data using the <script> tag.

You could also load the JSON data via an XMLHttpRequest and then turn it into an object via:

var dataObject = eval('(' + myJSONData + ')');

And if you can't verify how secure the data is, you can use the official JSON JavaScript parser (see here for more information) to validate the data before executing it.

Steve

Steve Harrison
I was trying to avoid using XMLHttpRequest as I gather it's heavier on network transactions? Thanks for the advice though, I'm confident about the source so am not using json2.js this time.
I'm not sure why an XMLHttpRequest would be any heavier than directly loading the file into the HTML page... I think it's a fairly common practice to use an XMLHttpRequest whenever you need to load XML/JSON data in JavaScript.
Steve Harrison