This can complicate your problem, so feel free to embed, but using a third-party Javascript library may ease your burden a bit (as the commentator noted). Prototype and jQuery have scope methods, such as the bind function in Prototype. Plus, you donβt have to write your own Ajax request code, although I recommend you dig in and see how it works! Prototype classes can help with area problems as you described. You may want to refactor to use asynchronous Ajax requests, although the browser does not need to wait, as it was with the way you wrote it. The following will display your warning message and set the variable accordingly, although I have not checked it for errors. It shows the basic concept.
var TestClass = Class.create(); TestClass.prototype = { MyVariable: null, AjaxURL: "http://yourajaxurl.com/something.asmx", DoAjaxCall: function() { new Ajax.Request(this.AjaxURL, method: 'get', onSuccess: this.AjaxCallback.bind(this), onFailure: this.DoSomethingSmart.bind(this)); }, AjaxCallback: function(returnVal) { this.MyVariable = returnVal.responseText;
jamesmillerio
source share