views:

144

answers:

1

I try to do Ajax tooltip via this jQuery plugin: http://jquery.bassistance.de/tooltip/demo/

I have some thing like this:

<p id="foottip">
  <span href="/last_votes/6">footnote</span>.
</p>

<script type="text/javascript">
  $(function() {
    $("#foottip span").tooltip({
      bodyHandler: function() {
        //dj ajax here and cache
        var tip = ''
        var url = $(this).attr("href");

        $.ajax({
          url:url, success:function(html){tip = html;}, async:false
        });

        return tip
      },

      showURL: false
    });
  })
</script>   

I do it with an asynchronous Ajax request, but the solution has a problem, sometimes it redirects the page. It seems to be a bug. How can I do an Ajax tooltip with an asynchronous request? I can't find way to pass result to the tooltip asynchronously.

+2  A: 

Yes, your code will not work in an asynchronous manner:

var tip = ''
var url = $(this).attr("href");
$.ajax({
    url:url, success:function(html){tip = html;}, async:true
});

// -- The problem here is that your bodyHandler function will return
// -- immediately, before the AJAX callback is called.
return tip

To solve this problem, you may have to put your tooltip rendering code inside the success callback of your $.ajax request.

Daniel Vassallo
Ajax is called synchronous (async:false). It wait for return. But as i sad it has a bug in this way. I Don't understand how to put tooltip initialization inside succes. Because tooltip must call my ajax request.
Evg
It's little bit strange.. because i must bind my ajax first on hover event and then do on return tooltip initialization.. but i will try it)
Evg
i'm not sure what onhover will work for tool tip. I mean i move mouse on area, onhover is done, ajax do processing, init tooltip, but no on hover for tooltip (because it's allready done). And no real result. I think tooltip init must be first anyway.
Evg
@Evg: You'right. It's the tooltip that has to trigger your AJAX request, so you can't use the method I suggested earlier. I have no experience with that plugin, but it may not be able to work with a async loading of the data... If the plugin has a method that you can call to show the tooltip, you may want to call this from the ajax success function.
Daniel Vassallo