0

I have a piece of JS code where i just have to connect the pieces together to make it work. My js code will load more content,fetched from database.Until now it is working just fine,when a user clicks a link,will load more.

Now i want to make it automatically load when the page is near the end. My code:

Load more function

function myFunction() {
    $(window).scroll(function () {
        if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
            $("load_more"); < ---- ** HOW DO I CALL THE LOAD FUNCTION ?**
    }
    });

    $(function () {
        $(document).on("click", ".load_more", function () {
            var last_msg_id = $(this).attr("id");
            var device =<? php echo $idevice; ?>;
            if (last_msg_id != 'end') {
                $.ajax({//Make the Ajax Request
                    type: "POST",
                    url: "index_more.php",
                    data: "lastmsg=" + last_msg_id + "&aa=" + device,
                    beforeSend: function () {
                        $('a.load_more').html('<img src="img/loading.gif" />');
                    },
                    success: function (html) {
                        $("#more").remove();
                        $("ol#updates").append(html);
                    }
                });

            }
            return false;
        });
    });

So,my question is how do i call the "load_more" function from $(window).scroll?

Thanks!

1
  • you do not need to wrap $(window).scroll(function () inside function... Commented Sep 27, 2013 at 7:56

1 Answer 1

3

Just trigger the click upon the button,

$(window).scroll(function () {
   if ($(window).scrollTop() >= ($(document).height() - $(window).height() - 10)) {
      $(".load_more").trigger('click'); 
   }
});

So some modification also needed,

$(function() {

    $(window).scroll(function () {
       if ($(window).scrollTop() >= ($(document).height() - $(window).height() - 10)) {
          $(".load_more").trigger('click'); 
       }
    });

    $(document).on("click",".load_more",function() {
        var last_msg_id = $(this).attr("id");
        var device=<?php echo $idevice; ?>;
        if(last_msg_id!='end'){
            $.ajax({//Make the Ajax Request
                type: "POST",
                url: "index_more.php",
                data: "lastmsg="+ last_msg_id+"&aa="+device,
                beforeSend:  function() {
                    $('a.load_more').html('<img src="img/loading.gif" />');
                },
                success: function(html){
                    $("#more").remove();
                    $("ol#updates").append(html);
                }
            });

        }
        return false;
    });
});

Hope this meets the requirement.

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

1 Comment

perfect,it did the job! Thank you.

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.