473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Re: Another AJAX question

On Jun 5, 9:36 pm, sheldonlg <sheldonlgwrote:
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.
Jun 27 '08 #1
4 1946
slebetman wrote:
On Jun 5, 9:36 pm, sheldonlg <sheldonlgwrote:
>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.
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.
Jun 27 '08 #2
On Jun 6, 7:47*am, sheldonlg <sheldonlgwrote:
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
Jun 27 '08 #3
beegee wrote:
On Jun 6, 7:47 am, sheldonlg <sheldonlgwrote:
>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?
Jun 27 '08 #4
On Jun 6, 10:21 am, sheldonlg <sheldonlgwrote:
beegee wrote:
On Jun 6, 7:47 am, sheldonlg <sheldonlgwrote:
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?

Yup. You have it. In this way you're emulating the classic client-
server application and keeping UI and data separate. If you don't
care about security too much, JSON is a even better bet than XML cause
there is no parsing involved on the Javascript side. You just eval
the result and you've got a javascript object.

Bob
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: G Matthew J | last post by:
interesting "signal vs. noise" blog entry: http://37signals.com/svn/archives2/whats_wrong_with_ajax.php
21
by: javainfo | last post by:
How can i refresh IFRAME and load data through AJAX?
9
by: darrel | last post by:
Last week I asked about ASP.net 2.0 AJAX frameworks and there appears to be several to choose from. I haven't used ASP.net 2.0 yet, but from doing a bit of reading, it appears that ASP.net 2.0...
31
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked...
1
by: codefragment | last post by:
Hi I have asked this on the official asp.net forums but no luck so thought I'd try here Assume you have a page with a filter control and a grid view and many other controls on it. When the...
8
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I'm about to finally make the jump and start a new site using AJAX. THe question i have for all of you AJAX developers out there is which one? 1. The Standard AJAX frame work 2. The Tool kit....
2
by: =?Utf-8?B?VG9u?= | last post by:
Hello, I want to understand teh benefits of ajax technology. Does anyone has a good website where AJAX EXTENSIONS is worked out so I really understand it. There a 2 main questions: 1) How about...
2
by: Cirene | last post by:
3 quick questions... 1. Are the controls in the AJAX Futures download "beta" or the release and stable version? 2. Also, where is the best way to learn how to implement these? (Other than by...
6
by: Jonathan Wood | last post by:
Greetings, I'd like to implement some AJAX features on an existing ASP.NET site. I have one example of doing this but, otherwise, don't know much about it. I have one question, though, about...
22
by: sheldonlg | last post by:
I am looking for a clean solution to a problem that I solved in, what I call, a "dirty" way. Here is what I want to do. I have a dropdown list. Clicking on an item in the dropdown list invokes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.