3

Have posted before but I think I was a little too specific to gain much interest Original. I am new to javascript and AJAX and have found it difficult to find a simple answer to this question. My goal is to simply parse some remote JSON, giving me an object which I can then use in the rest of application.

The problem I am having is keeping this object alive (getting data using XMLHttpRequest it is only populated during the callback function). I have jQuery available if this eases the answer. The javascript is part of a google chrome extention and does have the right permissions to access the remote JSON.

Hope someone can help

Edit; code from original post + Scott M's answer

var createdDate;

function checkDomainRegistrationLength(url) {
  var serverName = urlToServerName(url);

  var xhr = new XMLHttpRequest();

  xhr.open("GET", "eg.json", false); //Code now working, was set to async
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      var resp = JSON.parse(xhr.responseText);
      createdDate = resp.WhoisRecord.audit.createdDate;
    }
  }
  xhr.send();
  return createdDate;
}
3
  • Show some code that illustrates the problem you're having. Commented Feb 12, 2011 at 16:55
  • How long do you want to "keep it alive" and how long can you already? Until the next page reload/click on a non-JS link? Until the end of the session? Commented Feb 12, 2011 at 17:52
  • I just need to keep it alive to get it to return a value from the JSON, I think the problem itself is being masked by others since every bit of help works, but then reveals a different error. Commented Feb 12, 2011 at 17:56

1 Answer 1

0

assign the returned object to a global variable. You can then access it throughout the application.


edit: if you have jQuery available, it makes your life infinitely easier. Use the $.ajax() function to do the work for you. in the callback (which is only called once!) you can then set the variable.

sorry for not seeing the jquery reference earlier, or i would have included this in the answer.

Sign up to request clarification or add additional context in comments.

6 Comments

Again, it should be working but just won't... Long debug session ahead :(
yeah if that isnt working then you may have a problem somewhere else. try using Debug.print(message) (i think that's the right call) and viewing the output in firebug.
I ran it through the debugger and I notice the function itself runs more than once (expected just the xhr state change function to be accessed multiple times) and eventually returns the correct string. However, it returns a null value the first few times which is where I am seeing the problem. Any ideas why it may be executing more than once? (have checked for extra accidental calls)
added info about jquery. it will make your life so much easier. also, your function is being called multiple times because the ready state changes multiple times. jquery will abstract this for you (and make it work in all browsers) and make code much easier to write and more predictable.
Yeah, I've been reading up a little on it this afternoon. Will try to implement this using it's methods. Thanks for the help
|

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.