JavaScript Custom EventHandlers?

  • Thread starter Gueorguiev Stefan Gueorguiev
  • Start date
G

Gueorguiev Stefan Gueorguiev

Hi,

I have the following problem that I can't seem to find a solution to.

I have a jsp webserver and it creates webpages containing different things
and amongst them there is field with an image that should be changing
(like a slideshow). The problem is that I don't know beforehand the timing
of the changes. The image should change when an event occurs (e.g. a
person presses a key), but that event has to occur on the server (or on
another computer but definetely not on the client).
Now one solution is when the event occurs on the server to just generate
the html all over and send it again but I don't want to do that. I want
the image to change but I don't want the whole webpage to reload.

So I was wondering can a custom event handler be implemented in javascript
such that when some event happens elsewhere, not on the client, my
handler will catch it and perform the required operation?

Thanks in advance,
Stefan
 
K

kaeli

So I was wondering can a custom event handler be implemented in javascript

Javascript runs on the client. And only the client.
(excepting jscript.net)
such that when some event happens elsewhere, not on the client, my
handler will catch it and perform the required operation?

So, that would be a 'no'.
You can't trap events that are occuring somewhere other than the client from
the client.

You're dealing with a client-server situation here. Stateless. Once the
server is done processing and sends to the client, that's it.
You could implement some sort of timer on the client that polls the server
for changes and updates the image as appropriate, but you do have to initiate
the polling from the client to the server. You can't push from the server to
the client without some sort of request. After all, once it's done, the
packets are sent, the server has no idea which client to push TO.

This is assuming standard client-server web app architecture, since you
mentioned JSP and webserver and all.

--
--
~kaeli~
Found God? If nobody claims Him in 30 days, He's yours to
keep.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
G

Gueorguiev Stefan Gueorguiev

Ok I got it but there is one more thing then I have a follow-up question.
You could implement some sort of timer on the client that polls the server
for changes and updates the image as appropriate, but you do have to initiate
the polling from the client to the server.

Yeah I guess I'll have to do this, the question is how? I know how to do
the timing but how would I contact the server? So far I am familiar with
javascript functions that access only the current document. Are there any
that access an arbitrary server (e.g. MySQL) and pull out info from it? If
so, could you direct me to where I can find out more about this set of
function. I tried looking up on google but without much success.

Thanks,
Stefan
 
K

kaeli

I know how to do
the timing but how would I contact the server?

Various ways.
The newest is using xmlhttprequest. Search the archives here, as it's been a
common discussion lately.
If it isn't an option for you for whatever reason, you can make dynamic
script elements and have that source call a script on the server that returns
javascript.
I did that approach for some dynamic content and put up a little thing about
it here.

http://www.ipwebdesign.net/kaelisSpace/useful_dynamicSelectsDB.html
javascript functions that access only the current document. Are there any
that access an arbitrary server (e.g. MySQL)

No. Not exactly.
It has to call server-side script that does things.
Basically it's like a web service, kinda sorta. It just makes a request to a
web server. What the request does is up to you.

--
 
G

Gueorguiev Stefan Gueorguiev

Seems like xmlhttprequest is exactly what I needed! I just didn't
know it existed:)

Thanks a lot!
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top