![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
Dan Rumney wrote: 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); } |
#12
| |||
| |||
|
|
On Jun 5, 9:36 pm, sheldonlg <sheldonlg> wrote: Dan Rumney wrote: 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); } You can of course use closure to pass the required parameter: 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. |
#13
| |||
| |||
|
|
slebetman wrote: I don't understand. *How is the hideIt variable set (in the php script)?. *How does the javascript know that there is a variable hideIt that is coming back? *My line: var resp = postProcess(pObjRequest.responseText); is already inside an if (ajaxObject.readyState == 4) {} block that returns the string for the innerHTML. |
#14
| |||
| |||
|
|
On Jun 6, 7:47 am, sheldonlg <sheldonlg> wrote: slebetman wrote: I don't understand. How is the hideIt variable set (in the php script)?. How does the javascript know that there is a variable hideIt that is coming back? My line: var resp = postProcess(pObjRequest.responseText); is already inside an if (ajaxObject.readyState == 4) {} block that returns the string for the innerHTML. Okay. Asynchronous Javascript XML = AJAX. HTML is not a transport protocol and generating HTML on the server through PHP responses sort of contradicts the purpose of AJAX. Rethink your design. Use Javascript to generate markup from data, use XML or JSON for transporting that data. If you do this, some of the other poster's solutions will begin to make sense. I know there are tons of examples on the net of AJAH but, in my humble opinion, that's why they're called script kiddies. Bob |
#15
| |||
| |||
|
|
beegee wrote: On Jun 6, 7:47 am, sheldonlg <sheldonlg> wrote: slebetman wrote: I don't understand. How is the hideIt variable set (in the php script)?. How does the javascript know that there is a variable hideIt that is coming back? My line: var resp = postProcess(pObjRequest.responseText); is already inside an if (ajaxObject.readyState == 4) {} block that returns the string for the innerHTML. Okay. Asynchronous Javascript XML = AJAX. HTML is not a transport protocol and generating HTML on the server through PHP responses sort of contradicts the purpose of AJAX. Rethink your design. Use Javascript to generate markup from data, use XML or JSON for transporting that data. If you do this, some of the other poster's solutions will begin to make sense. I know there are tons of examples on the net of AJAH but, in my humble opinion, that's why they're called script kiddies. Bob I **think** I understand what you are saying. This application happens to be for recipes, and the dropdown list is a list of recipes. That dropdown list is not created through AJAX. Now, when the user clicks on a particular recipe, I send an AJAX call. On the server it looks up the data for the recipe from the database. What I then do now is to generate the entire html portion of the recipe to fit into a div on the bottom of the page (via innerHTML). I guess what you are saying is to send back an xml string containing the data and use javacript on the browser to process that xml string, placing all the data into the appropriate tags on the bottom of the page (since the bottom of the page always has the same fields). In that case, including another field in the XML would, indeed, be trivial. Am I reading you correctly? |
#16
| |||
| |||
|
|
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); } |
#17
| |||
| |||
|
|
On Jun 5, 3:36 pm, sheldonlg <sheldonlg> wrote: 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); } What's wrong with that ? I'd say that that's perfect. Why don't you like it this way ? |
#18
| |||
| |||
|
|
What's wrong with that ? I'd say that that's perfect. Why don't you like it this way ? I get an uncomfortable feeling when I do a quick-and-dirty like this, just to get it tow work. *I like the cleaner approach that was told to me in another response in this thread. *There, on the server, I would prepare an XML statement to pass back all the data for the bottom of the page. *I would also add in a field, <showEdit> with the value of 0 or 1 inserted for the data. *At the browser I would then process the XML to get ALL the data, including this field, and do what was necessary for all the fields. |
#19
| |||
| |||
|
|
On Jun 7, 8:31 pm, sheldonlg <sheldonlg> wrote: What's wrong with that ? I'd say that that's perfect. Why don't you like it this way ? I get an uncomfortable feeling when I do a quick-and-dirty like this, just to get it tow work. I like the cleaner approach that was told to me in another response in this thread. There, on the server, I would prepare an XML statement to pass back all the data for the bottom of the page. I would also add in a field, <showEdit> with the value of 0 or 1 inserted for the data. At the browser I would then process the XML to get ALL the data, including this field, and do what was necessary for all the fields. Rewrite it all, server and client-side, and send it as an XML doc just because you need a boolean to enable/disable a single button ? Hmm, you must be kidding, that's not a good idea, imho. --Jorge. |
#20
| |||
| |||
|
|
But just for the sake of changing something : function postProcess(txt) { var s= document.getElementById('showEdit').style; s.display = (txt.substring(0,1) === "0") ? 'none' : ''; return txt.substring(1); } |
![]() |
| Thread Tools | |
| Display Modes | |
| |