T.J., I was surprised too that the DB would have time to get an update
before the page is unloaded, but I had a closer look at the
example :o)
If the links urls are downloadable file types like the ".exe" of the
example, the browser will not unload the current page, and the Ajax
request will complete.

Perhaps Safari does unload the page. You may try to specify a target
to the link to force safari to not replace the current page.

Also, I thought that the onclick handler should return a true value
for the link being followed...
Shouldn't you write something like ".... onclick = "return
logClick()"..." and add a "return true" at the end of your function?
[1]

The unintrusive approach of Fabien is probably the best here too
(well, not as good as the server side one but if you don't control the
server side...).
However, instead of creating an event for each link, you may observe
the document and use the event bubbling.
It would look like this (untested):
document.observe("click", function(event){
  var link = event.findElement("a.log");
  if(link)
  {
    new Ajax.Request('/logger.php?action=1');
  }
});

you may use the link variable to get the href value of the link and
send it to your ajax script.

Advantages are:
- Only one event listener (may cause a difference on IE's memory
leaks)
- It will work with dynamically added links

I removed Fabien's trick to wait the Ajax's return before changing the
location because I think the target attribute will fix the issue[2],
but feel free to use it if there is no other choice (safari users will
have a crappy experience, but it will work...).

Once again, try first to have a server side solution. It will work
better.

Eric

[1] Well, what you "really should" do is using "observe" :o)
[2] and like T.J. said, it will reduce user experience.

On Aug 11, 3:02 am, "T.J. Crowder" <[email protected]> wrote:
> Hi,
>
> By default, Ajax.Request is *asynchronous*. I'm surprised, frankly,
> that you're seeing many database updates at all, regardless of the
> browser, since a request may not have been transmitted before the page
> gets torn down to make way for the new one. I wouldn't expect it to be
> reliable.
>
> If you made the requests synchronous, it would likely be fairly
> reliable, but the user experience would be seriously degraded -- the
> entire browser (in most cases) would appear to lock up for a second or
> two before the link was followed. And of course, if the user has
> JavaScript disabled, it's game over.
>
> As Fabien mentioned, the reliable way to do this is server-side with a
> redirect. His second suggestion (handling the click event, cancelling
> it, sending off an Ajax request, and then triggering the location when
> the request completes) will again result in a degraded user experience
> on multiple levels -- delays (though at least the browser wouldn't
> lock up), shift- and ctrl- click not handled correctly, etc.
>
> HTH,
> --
> T.J. Crowder
> Independent Software Consultant
> tj / crowder software / comwww.crowdersoftware.com
>
> On Aug 10, 11:34 pm, JoJo <[email protected]> wrote:
>
> > I'm collecting stats on user clicking behavior. In this example, I
> > write an entry into the database whenever someone clicks a link that
> > starts a download:
>
> > <a href="program.exe" onclick="logClick()">download program</a>
>
> > function logClick() {
> >    new Ajax.Request(
> >       '/logger.php?action=1'
> >    );
>
> > }
>
> > This works great in Internet Explorer and Firefox. In Firefox, my
> > Firebug highlights the request in red as if it's an error. Anyway, I
> > see my database being updated, so I'm not too worried. What I'm
> > worried about is this fails to work in Safari 4. The only way I can
> > get it to work is:
>
> > function logClick() {
> >    new Ajax.Request(
> >       '/logger.php?action=1', {
> >          onSuccess: function(transport) {
> >             alert(transport.responseText);
> >          }
> >       }
> >    );
>
> > }
>
> > For some reason, this causes the database to be written to.
> > Transport.responseText is blank for unknown reasons. How do I get this
> > to work without the alert? The alert degrades the user experience.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to