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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.