> I'm doing an Ajax load, and I'd like to scroll to the top of the div
> containing the new content after the Ajax load is completed. But there
> doesn't seem to be a way to do that yet in JQuery. Even if I put the
> scrollto command in the callback, it gets ignored because (I think) the
> load is still being completed. Here's the code I've been using:
Have you tried debugging this?
> function ScrollToDiv(theDivID)
> {
> $("html,body").animate({ scrollTop: $("#"+theDivID).offset().top });
> }
Why not just set the location hash to test it? Then you will know the
callback worked. What does it mean to scroll both the html and body
elements? Does jQuery even support animated scrolling?
> $(document).ready(function(){
>
[quoted text clipped - 7 lines]
>
> jQuery likes to be compatible with other javascript libraries, and it
Does it?
> seems like Scriptaculous has an ajax load command that can be set to
jQuery and Prototype and Scriptaculous? Whave are you up to in terms
of script weight? About 200K? Is an animated scroll effect really
worth all that?
> synchronous rather than asynchronous. So I'm trying this:
jQuery can do synchronous Ajax requests, but that is the last thing
you want to do.
> jQuery.noConflict();
>
[quoted text clipped - 14 lines]
>
> But Firebug gives me an error message saying, "Ajax is not defined,"
So much for compatibility.
> even though I have included the Scriptaculous and Prototype libraries.
>
> How can I fix this?
Start by debugging the original jQuery failure. Don't throw two more
libraries at the problem.
Vik Rubenfeld - 31 Oct 2007 03:31 GMT
Thanks very much for this good advice, David.
> Have you tried debugging this?
>
> > function ScrollToDiv(theDivID)
> > {
> > $("html,body").animate({ scrollTop: $("#"+theDivID).offset().top });
> > }
I got that code from a posting on a forum, and it may very well have
bugs in it. It seems to be working now. I got it all working like so:
function ScrollToDiv(theDivID)
{
$("html,body").animate({ scrollTop: $("#"+theDivID).offset().top });
}
$(document).ready(function(){
$("#HelpInfo").click(function(){
var htmlStr = $(this).html();
$("#HelpInfo").load("http://localhost:8888/index.php/your_plan/HelpInfo",
{contents: htmlStr}, function(){ ScrollToDiv("HelpInfo")});
});
});
> Why not just set the location hash to test it? Then you will know the
> callback worked.
I appreciate this - because it gives me something to learn about. I
don't even know what a location hash is yet. :)
> What does it mean to scroll both the html and body
> elements?
Good question - I'll look into it.
> Does jQuery even support animated scrolling?
It does!
> jQuery and Prototype and Scriptaculous? Whave are you up to in terms
> of script weight? About 200K?
You're quite right. I'll make things work with just one library.
Peter Michaux - 31 Oct 2007 04:05 GMT
> Thanks very much for this good advice, David.
>
[quoted text clipped - 28 lines]
> I appreciate this - because it gives me something to learn about. I
> don't even know what a location hash is yet. :)
I'd be very careful about setting the location hash. Recent versions
of Safari don't like this very much and go into some buggy loading
state that is fixed in the newest versions. Other browsers may do a
page reload when setting any part of the location object.
Peter