How do you set a Javascript attachEvent to listen for a request/response from the server

I have a page that uses Javascript in which I need to have a function that listens for data to be received from the server. Basically, anytime it hits the server, I need the function to fire.

This seems like it would be pretty straightforward - but so far nothing I've tried works.

I know I have the basic set-up correct - because I can get it to listen for mouse up, mouse down, and load - but nothing for when it sends or receives data from the server.

For example, this works...

document.attachEvent('onmouseup', function(event) {
   alert("mouse released!");        
});
So does this...

window.attachEvent('onload', function(event) {
   alert("page loaded!");      
});
However, none of these do...

document.attachEvent('onreceive', function(event) {
   alert("data received!");        
});

window.attachEvent('onreceive', function(event) {
   alert("data received!");        
});

document.attachEvent('onpost', function(event) {
   alert("data posted!");      
});

window.attachEvent('onpost', function(event) {
   alert("data posted!");      
});

document.attachEvent('onmessage', function(event) {
   alert("data posted!");      
});

window.attachEvent('onmessage', function(event) {
   alert("data posted!");      
});
Any suggestions?

Thanks in advance!
tmitch1968Asked:
Who is Participating?
 
Michel PlungjanIT ExpertCommented:
Normally the way Ajax works is that it triggers the callback. That is done when the request completes

Perhaps you want reverse Ajax? http://directwebremoting.org/dwr/documentation/reverse-ajax/index.html
0
 
Dave BaldwinFixer of ProblemsCommented:
I don't believe 'onpost' and 'onreceive' are valid javascript events.  http://www.w3schools.com/jsref/dom_obj_event.asp   'onmessage' is a new one on me:  http://www.w3schools.com/jsref/event_onmessage_sse.asp

After a page is loaded, the only time that I know of that it 'hits the server' is thru an AJAX call.  Otherwise all the contact with the server is done.
0
 
tmitch1968Author Commented:
I think you may be right; I'm not sure because I'm relatively new to ajax.  

Does an 'ajax' call necessarily mean you need to see the word 'ajax' in the javascript code?  

I don't see the word 'ajax' anywhere - but I know that functions within the code (java code) on the server are being accessed.  I can even put breakpoints within the java code as I run it and see where it enters the functions.  However, I know that the page is not being fully loaded when it comes back - because

window.attachEvent('onload', function(event) {
   alert("page loaded!");      
});

is fired when the page first loads - but not when it returns from any of the functions within the server code.

Assuming that this is ajax - is there a general way to tell when it is sending to and from the server?  

I could do it every place I see the server functions being called within the javascript - but I was hoping there was a listening event so I would only have to do this once.

Thanks again
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Dave BaldwinFixer of ProblemsCommented:
Here http://www.w3schools.com/ajax/default.asp are the basics of AJAX.  It is based on the XMLHttpRequest object.  And the word 'AJAX' is not in the code.  It is a made-up name that stands for Asynchronous JavaScript and XML.  Which is also misleading because that is not all there is to it.

The AJAX code includes a specific listener that determines when the results from the server are available.

I'm curious what you are calling 'server functions'.  ??
0
 
tmitch1968Author Commented:
By server functions, I just mean the functions within the java code of the back-end.
0
 
Dave BaldwinFixer of ProblemsCommented:
What Java code?  If you are running JSP pages, you must understand that javascript and JAVA have nothing at all to do with each other.  They are completely separate languages that have the word 'java' in the name.  Javascript runs only in the browser and JAVA runs on the server.
0
 
Michel PlungjanIT ExpertCommented:
You may be interested in something like DWR: http://directwebremoting.org/dwr/index.html
0
 
tmitch1968Author Commented:
Hi Michael,

Yep; that's what I'm already using - dwr.

So, it looks like I'm already using ajax - I just expected to see the keyword ajax somewhere.

Dave - just to clear up some of the confusion - yes, I totally understand javascript and Java are two different languages.

The way this particular dwr app works is that it has a front-end with javascript - and a back-end with Java.

Within the javascript, there are references to the back-end Java objects.  It references these objects whenever it needs to call a function on the back-end.  


So, back to the original question - in a dwr web application, is there any way to set a listener to detect when one of the front-end sends or receives something from the back-end?
0
 
Dave BaldwinFixer of ProblemsCommented:
Someone else will have to help you then.  I never heard of DWR until now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.