Synchronuos server callback in a standard way

A

Andrus

User enters an item code to a browser <input type=text> field.
I need to verify the code entered using server callback.

If item code is wrong, user is not allowed to leave the field.
So I think I must use synchronuous server callback for this. Data passed to
server is larger tham 2KB, so POST method should be used.

Unfortunately, W3C standard does not support synchrunuous server callbacks.
It allows using <iframe src=> trick or <script src=> trick by changing src=
attribute dynamically.
Both of them are asynchronous.

IE has Microsoft.XMLHTTP object and Mozilla its clone, XMLHttpRequest()
function. However, those non-standard extensions will not work in other
browsers.

It it possible to make synchronuous server callbacks in a standard way ?

aAndrus.
 
G

Grant Wagner

Andrus said:
User enters an item code to a browser <input type=text> field.
I need to verify the code entered using server callback.

If item code is wrong, user is not allowed to leave the field.
So I think I must use synchronuous server callback for this. Data passed to
server is larger tham 2KB, so POST method should be used.

Unfortunately, W3C standard does not support synchrunuous server callbacks.
It allows using <iframe src=> trick or <script src=> trick by changing src=
attribute dynamically.
Both of them are asynchronous.

IE has Microsoft.XMLHTTP object and Mozilla its clone, XMLHttpRequest()
function. However, those non-standard extensions will not work in other
browsers.

It it possible to make synchronuous server callbacks in a standard way ?

aAndrus.

It's possible, but it's still dependant on client-side JavaScript being enabled,
and it requires more work on the server.

var serverCallBack = new Image();
serverCallBack.onload = callBackResponse;
serverCallBack.onerror = callBackNoResponse;
function callBackResponse() {
if (this) {
switch (this.width) {
case 1:
// do something
break;
case 2:
// do something else
break;
// etc
}
}
}
function callBackNoResponse() {
alert('Could not validate your input.');
}

Then in your HTML, do something like:

<input type="text" onchange="serverCallBack.src='yourValidator.cgi?data=' +
this.value;" />


Now on the server, "yourValidator.cgi" will have to build (or read) and return
content to the browser of Content-type: image/gif (or image/jpeg) with specific
widths for each "response code" you want.

In other words, a gif of width 1 would be for correct input, width 2 would be
for input that's blank, width 3 would be for input that contains incorrect
characters, etc.

By the way, this is still asynchronous, if the user managed to trigger the
onchange event in a second input while the server was still dealing with the
first, it would cause a second asynchronous retrieval of "yourValidator.cgi".
This could cause the onload events for serverCallBack to fire out of order (or
only once for two retrievals). There are ways to avoid that problem as well by
having a "pool" of serverCallBack objects and cycle through them, each with it's
own onload event. If you had say, 10 of them, it's highly unlikely that a human
could cause retrievals fast enough to cause the first one to be used again
before it returned some sort of result.

You've already said that you wanted a more cross-browser solution (ie - one that
didn't use the XML HTTP Request object), so I'm assuming this is for the
Internet? If so, as I've already said, the above functionality would only work
if the user has client-side JavaScript enabled.

Ultimately it might be better to just POST the form and have the server validate
everything at once.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,076
Messages
2,570,565
Members
47,200
Latest member
Vanessa98N

Latest Threads

Top