Orange Kid
Orange Kid

Reputation: 544

Javascript - Run function *after* another function runs on load?

I'm using scrollsaver.js (http://en.hasheminezhad.com/scrollsaver) to maintain scroll positions of elements on postback. There are instances where I wish to reset scroll position though, and I am having trouble doing so.

Paging through a list, I wish to scroll to the top on each page change. I do this by registering a call to resetGridScroll (below) in the paging handler.

scrollsaver.js ties to the page load event. When I do the same, my function runs before scrollsaver.js loads positions. Is there a way to force mine to run after page load?

function resetGridScroll() {
    $(document).ready(function () {
        $("div.items").animate({ scrollTop: 0, easing: 'swing', queue: false }, 15000);
    });
}

EDIT: I ended up clearing the cookie scrollsaver.js sets to store scroll positions. It gets cleared before the script loads so all positions are reset. Not elegant, but workable.

Upvotes: 0

Views: 973

Answers (2)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76870

If you need to load an external js script and execute code afer it you could use $.getScript() and put the code in the callback:

$.getScript("scrollsaver.js", function(){
    //code to execute after the script has loaded here
});

Upvotes: 2

Manse
Manse

Reputation: 38147

Put the function that calls the resetGridScroll() within :

$(document).ready(function () {
 //here
});

Not your function being called for example :

$(document).ready(function () {
   resetGridScroll();
});


function resetGridScroll() {
    $("div.items").scrollType(0);
    $("div.items").animate({ scrollTop: 0, easing: 'swing', queue: false }, 15000);
}

Upvotes: 0

Related Questions