Dan Rumney wrote:You can of course use closure to pass the required parameter:sheldonlg wrote:I am looking for a clean solution to a problem that I solved in, what
I call, a "dirty" way.[snip]Could you provide your dirty code, please?
Sure (in a mixture of code and pseudocode):
****************
Javascript processing:
(...the important two lines in handling the response...)
var resp = postProcess(pObjRequest.responseText);
document.getElementById('foo').innerHTML = resp;
function postProcess(txt) {
var hideIt = txt.substring(0,1);
var editButton = document.getElementById('showEdit');
if (hideIt == 0)
editButton.style.display = 'none';
else
editButton.style.display = '';
return txt.substring(1);
}
var hideIt; // the variable you wish to pass
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (hideIt) {// here you can use the variable..}
}
}
If you want to pass the value rather than the variable itself:
var hideIt;
(function (myHideIt) {
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (myHideIt) {// check the passed value..}
}
}
})(hideIt) // pass the value of hideIt
Or if you prefer, you can wrap this up in a function that generates a
function:
function makeAjaxCallback (ajaxObject,myHideIt) {
return function () {
if (ajaxObject.readyState == 4) {
if (myHideIt) {// check the passed value..}
}
}
}
ajax.onreadystatechange = makeAjaxCallback(ajax,hideIt);
There are many ways to use closure to do this depending on exactly
what you want and, to some degree, preferred style.