# Javascript event handling after ajax call

**URL:** <https://www.ruby-forum.com/t/javascript-event-handling-after-ajax-call/144835>\
**Category:** Rails\
**Created:** [August 2, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/javascript-event-handling-after-ajax-call/144835 "2008-08-02T21:49:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 2, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/javascript-event-handling-after-ajax-call/144835/1 "2008-08-02T21:49:07Z")

</div>

I’m using link\_to\_remote to populate a div with some data from the db  
and then show the div. The problem is that I need to show it  
reasonably close to the place where I clicked. As you know, Firefox  
passes its event object to each event handler and because the onclick  
event is taken by the Ajax call, I don’t have access to the event to  
extract the mouse coordinates anytime in the process.

I’m thinking of doing link\_to\_function instead and then using my own  
function to make the AJAX call, but it’s not portable and I don’t like  
it. Another solution I’ve thought of is attaching a ‘click observer’  
for each link that uses link\_to\_remote but there could be potentially  
many of those – so I’m not crazy about this idea either.

Anybody have better ideas?

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 2, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/javascript-event-handling-after-ajax-call/144835/2 "2008-08-02T22:03:45Z")

</div>

On 2 Aug 2008, at 20:48, surge wrote:

> I’m using link\_to\_remote to populate a div with some data from the db  
> and then show the div. The problem is that I need to show it  
> reasonably close to the place where I clicked. As you know, Firefox  
> passes its event object to each event handler and because the onclick  
> event is taken by the Ajax call, I don’t have access to the event to  
> extract the mouse coordinates anytime in the process.
> 
> I’m thinking of doing link\_to\_function instead and then using my own  
> function to make the AJAX call, but it’s not portable and I don’t like

You can always just generate the same javascript as link\_to\_remote  
with (that just sets up an Ajax.Updater or and Ajax.Request) if you’re  
worried about working on different browsers and so on (the cleverness  
for that isn’t in link\_to\_remote, it’s in the prototype javascript  
library that it calls).

This sort of thing is rather easier when you write the javascript by  
hand, however you could probably do it still using link\_to\_remote.  
I’m not entirely sure what you’re trying to do, but if you were trying  
to submit the event coordinates with the request, then :with =\>  
"‘x=’+event.pointerX()+’&y=’+event.pointerY() should do the trick

Fred

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 3, 2008, 1:40am UTC](https://www.ruby-forum.com/t/javascript-event-handling-after-ajax-call/144835/3 "2008-08-03T01:40:09Z")

</div>

> You can always just generate the same javascript as link\_to\_remote  
> with (that just sets up an Ajax.Updater or and Ajax.Request) if you’re  
> worried about working on different browsers and so on (the cleverness  
> for that isn’t in link\_to\_remote, it’s in the prototype javascript  
> library that it calls).

Yep, that’s what I was talking about when I said I was going to write  
my own function to make the call. By “not portable” I meant that if  
the definition of the function provided by Rails changes somehow  
(e.g., it gets improved somehow), I would have to update my copy as  
well. So, that’s not ideal.

> to submit the event coordinates with the request, then :with =\>  
> "‘x=’+event.pointerX()+’&y=’+event.pointerY() should do the trick

That is a true gem of Rails wisdom and it’s exactly what I was looking  
for! The only trouble is detecting whether it’s Firefox or IE since  
they handle events differently, but it’s doable I think. Thanks a lot,  
Fred!

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 3, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/javascript-event-handling-after-ajax-call/144835/4 "2008-08-03T13:10:05Z")

</div>

On 3 Aug 2008, at 00:39, surge wrote:

> the definition of the function provided by Rails changes somehow  
> (e.g., it gets improved somehow), I would have to update my copy as  
> well. So, that’s not ideal.

I think that’s unlikely. Prototype might change internally but the  
public interface is unlikely to change any time soon.

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 3, 2008, 2:14am UTC](https://www.ruby-forum.com/t/javascript-event-handling-after-ajax-call/144835/5 "2008-08-03T02:14:38Z")

</div>

Just tried it and it worked like a charm. Thanks once again!!!
