1
<script type="text/javascript">
    function pageLoad() {
        var $scrollingDiv = $("#scrollfix");
        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height()) {
                __doPostBack('<%= GetMoreResults.UniqueID %>', '');
            }
            $scrollingDiv
                .stop()
                .animate({ "marginTop": ($(window).scrollTop()) + -60 + "px" }, "slow");
        });
    }
</script>

This code does not execute at all, the reason i'm using the pageLoad function is because the jquery code failed to execute after updatepanel partial postback. But then after using this one, none of the code above works even on first page startup.

However, the code that USED to work on page load is below but the jquery part STOPPED working after postback:

$(document).ready(function () {
    $().ready(function () {
        var $scrollingDiv = $("#scrollfix");
        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height()) {
                __doPostBack('<%= GetMoreResults.UniqueID %>', '');
            }
            $scrollingDiv
            .stop()
            .animate({ "marginTop": ($(window).scrollTop()) + -60 + "px" }, "slow");
        });
    });
});

Solutions? Thanks alot.

UPDATE #2

This is my current code: It works the first time the page loads, but not after the partial postback trigger by the __doPostBack.

<script type="text/javascript">
        window.load = pageLoad();
            function pageLoad() { 
                var $scrollingDiv = $("#scrollfix"); 
                $(window).scroll(function () { 
                    if ($(window).scrollTop() == $(document).height() - $(window).height()) { 
                        __doPostBack('<%= GetMoreResults.UniqueID %>', ''); 
                    } 
                    $scrollingDiv 
                    .stop() 
                    .animate({ "marginTop": eval($(window).scrollTop()) + -60 + "px" }, "slow"); 
                }); 
            } 
</script>

UPDATE #3

I should mention that this page does not inherit from Page, I've made a custom page class called BasePage : Page.

Maybe pageLoad() does not fire for some reason related to this?

4
  • 3
    Do you even call pageLoad() anywhere? Commented Nov 3, 2011 at 17:08
  • 1
    You can remove $().ready(function () { Commented Nov 3, 2011 at 17:09
  • Be more specific, what is not working, can you alert inside .scroll function? Commented Nov 3, 2011 at 17:12
  • pageLoad() is called automagically if you're using ASP.NET ajax. Commented Nov 3, 2011 at 17:23

3 Answers 3

2

Try using .bind() with your pageLoad:

pageLoad:

It is also called after every partial postback. It basically functions as a combination of Application.Init and PageRequestManager.EndRequest.

   function pageLoad() {
        var $scrollingDiv = $("#scrollfix");
        $(window).bind('scroll', function() {
            if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            __doPostBack('<%= GetMoreResults.UniqueID %>', '');
            }
            $scrollingDiv
            .stop()
            .animate({ "marginTop": ($(window).scrollTop()) + -60 + "px" }, "slow");
        });
    }

$(document).ready() and pageLoad() are not the same!

Sign up to request clarification or add additional context in comments.

3 Comments

Nope I tried the same thing with bind and live, doesn't execute.
Hey Rick, thanks again for the answer, I tried it with pageLoad not with $(document).ready(), that's whats confusing me. It used to work on other projects. I'm gonna try to narrow down the code to check if pageLoad is called each time.
turns out I do have a conflict, using pageLoad() elsewhere.
1

That code won't execute unless you assign it to the window.onload event:

window.onload = pageLoad; 

I would also consider wrapping the calculation of the top margin with the eval function:

eval($(window).scrollTop() - 60) + "px"

Lastly, I don't see any need for the second ready state function:

$().ready(function () {

2 Comments

I think the pageLoad() fuction is provided by the ASP.NET ScriptManager and can be used anywhere automatically. But I will try that. Thanks.
get rid of the () after pageLoad; you are assigning a function variable to another, not executing a function.
0

JavaScript knows nothing about partial page postbacks; that's an ASP.NET wrapper around an AJAX form post that returns HTML content. If you want your pageLoad to be called after partial page postbacks, you'll have to register a client script that calls pageLoad in your postback handler. You'll also need to ensure that the function is called upon your first page load, as @James Johnson pointed out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.