Javascript loading more content with AJAX problem












0















So I've been learning javascript for 2 months now and I decided to make a scroll thing where when you scroll the page you get more content (more paragraphs in div). I watched tutorial on youtube and did something like that but it doesn't work as i expected. At first it looks like it works but i noticed that i don't need to scroll down(or to scroll to bottom) to load more content but can go scroll up as well. To save you some time (if not interested in reading whole script) this is what i am checking (code near the end of the script):



 $(window).scrollTop() + $(window).height() > $("#load_data").height()


Which is in Vanilla JS from my understanding this:



 window.innerHeight + window.scrollY > document.querySelector('#load_div').offsetHeight


scrollTop() and scrollY should return(or set) value of pixels from the top of window(in this case) scrolled. Then we add inner height of window. Here is what i don't understand then, Shouldn't those 2 always be bigger then the divs height? So that explains why wherever i scroll it triggers. But how to set this up so it only triggers when i am at the bottom or near it? Here is full code (without php response part which only returns paragraph that got data from database):



            <div id="load_data"></div>
<div id="load_data_message"></div>

</div>

<script>
$(document).ready(function(){
var limit = 8;
var start = 0;
var action = 'inactive';
function load_data(limit, start){

$.ajax({
url: 'infinitload.php',
method: 'POST',
data: {limit: limit, start: start},
cache: false,
success: function(data){
$('#load_data').append(data); //Appending data;

if(data == ''){
$('#load_data_message').html('<button>No results found</button>');
action = 'active';//if
}else{
$('#load_data_message').html('<button>Loading Content</button>');
action = 'inactive';
}//else

}//success
})//ajax
}//load_data()

//Loading for the first time
if(action == 'inactive'){
action = 'active';
load_data(limit, start);
}

$(window).scroll(function(){
if( $(window).scrollTop() + $(window).height() > $("#load_data").height() && action == 'inactive'){
$('button:contains("Loading Content")').toggle();
action = 'active';
start = start + limit;
setTimeout(function(){
load_data(limit, start);
}, 1500);
}
});
});//dom ready

</script>


</body>


UPDATE:
I tried code like this http://jsfiddle.net/nick_craver/gWD66/
which totally makes sense and it works in example there but not in my case. Confused. It's probably something else leading to my problem so i ll try to look into my code again. Thanks for all replies.










share|improve this question

























  • Does the entire page scroll, or does a subsection of the page scroll?

    – Taplar
    Nov 20 '18 at 21:42











  • Thanks for the reply. Entire page

    – Vukasin Sevo
    Nov 20 '18 at 21:48











  • One approach I have done in the past is to throw an empty span after the container that the loaded content will be appended to. Then it is a pretty simple check to see if the offsetTop of that element is >= the scroll distance of the window. If it's >=, the user has scrolled enough so that the element is in their view, indicating more content needs to be added.

    – Taplar
    Nov 20 '18 at 21:51
















0















So I've been learning javascript for 2 months now and I decided to make a scroll thing where when you scroll the page you get more content (more paragraphs in div). I watched tutorial on youtube and did something like that but it doesn't work as i expected. At first it looks like it works but i noticed that i don't need to scroll down(or to scroll to bottom) to load more content but can go scroll up as well. To save you some time (if not interested in reading whole script) this is what i am checking (code near the end of the script):



 $(window).scrollTop() + $(window).height() > $("#load_data").height()


Which is in Vanilla JS from my understanding this:



 window.innerHeight + window.scrollY > document.querySelector('#load_div').offsetHeight


scrollTop() and scrollY should return(or set) value of pixels from the top of window(in this case) scrolled. Then we add inner height of window. Here is what i don't understand then, Shouldn't those 2 always be bigger then the divs height? So that explains why wherever i scroll it triggers. But how to set this up so it only triggers when i am at the bottom or near it? Here is full code (without php response part which only returns paragraph that got data from database):



            <div id="load_data"></div>
<div id="load_data_message"></div>

</div>

<script>
$(document).ready(function(){
var limit = 8;
var start = 0;
var action = 'inactive';
function load_data(limit, start){

$.ajax({
url: 'infinitload.php',
method: 'POST',
data: {limit: limit, start: start},
cache: false,
success: function(data){
$('#load_data').append(data); //Appending data;

if(data == ''){
$('#load_data_message').html('<button>No results found</button>');
action = 'active';//if
}else{
$('#load_data_message').html('<button>Loading Content</button>');
action = 'inactive';
}//else

}//success
})//ajax
}//load_data()

//Loading for the first time
if(action == 'inactive'){
action = 'active';
load_data(limit, start);
}

$(window).scroll(function(){
if( $(window).scrollTop() + $(window).height() > $("#load_data").height() && action == 'inactive'){
$('button:contains("Loading Content")').toggle();
action = 'active';
start = start + limit;
setTimeout(function(){
load_data(limit, start);
}, 1500);
}
});
});//dom ready

</script>


</body>


UPDATE:
I tried code like this http://jsfiddle.net/nick_craver/gWD66/
which totally makes sense and it works in example there but not in my case. Confused. It's probably something else leading to my problem so i ll try to look into my code again. Thanks for all replies.










share|improve this question

























  • Does the entire page scroll, or does a subsection of the page scroll?

    – Taplar
    Nov 20 '18 at 21:42











  • Thanks for the reply. Entire page

    – Vukasin Sevo
    Nov 20 '18 at 21:48











  • One approach I have done in the past is to throw an empty span after the container that the loaded content will be appended to. Then it is a pretty simple check to see if the offsetTop of that element is >= the scroll distance of the window. If it's >=, the user has scrolled enough so that the element is in their view, indicating more content needs to be added.

    – Taplar
    Nov 20 '18 at 21:51














0












0








0


1






So I've been learning javascript for 2 months now and I decided to make a scroll thing where when you scroll the page you get more content (more paragraphs in div). I watched tutorial on youtube and did something like that but it doesn't work as i expected. At first it looks like it works but i noticed that i don't need to scroll down(or to scroll to bottom) to load more content but can go scroll up as well. To save you some time (if not interested in reading whole script) this is what i am checking (code near the end of the script):



 $(window).scrollTop() + $(window).height() > $("#load_data").height()


Which is in Vanilla JS from my understanding this:



 window.innerHeight + window.scrollY > document.querySelector('#load_div').offsetHeight


scrollTop() and scrollY should return(or set) value of pixels from the top of window(in this case) scrolled. Then we add inner height of window. Here is what i don't understand then, Shouldn't those 2 always be bigger then the divs height? So that explains why wherever i scroll it triggers. But how to set this up so it only triggers when i am at the bottom or near it? Here is full code (without php response part which only returns paragraph that got data from database):



            <div id="load_data"></div>
<div id="load_data_message"></div>

</div>

<script>
$(document).ready(function(){
var limit = 8;
var start = 0;
var action = 'inactive';
function load_data(limit, start){

$.ajax({
url: 'infinitload.php',
method: 'POST',
data: {limit: limit, start: start},
cache: false,
success: function(data){
$('#load_data').append(data); //Appending data;

if(data == ''){
$('#load_data_message').html('<button>No results found</button>');
action = 'active';//if
}else{
$('#load_data_message').html('<button>Loading Content</button>');
action = 'inactive';
}//else

}//success
})//ajax
}//load_data()

//Loading for the first time
if(action == 'inactive'){
action = 'active';
load_data(limit, start);
}

$(window).scroll(function(){
if( $(window).scrollTop() + $(window).height() > $("#load_data").height() && action == 'inactive'){
$('button:contains("Loading Content")').toggle();
action = 'active';
start = start + limit;
setTimeout(function(){
load_data(limit, start);
}, 1500);
}
});
});//dom ready

</script>


</body>


UPDATE:
I tried code like this http://jsfiddle.net/nick_craver/gWD66/
which totally makes sense and it works in example there but not in my case. Confused. It's probably something else leading to my problem so i ll try to look into my code again. Thanks for all replies.










share|improve this question
















So I've been learning javascript for 2 months now and I decided to make a scroll thing where when you scroll the page you get more content (more paragraphs in div). I watched tutorial on youtube and did something like that but it doesn't work as i expected. At first it looks like it works but i noticed that i don't need to scroll down(or to scroll to bottom) to load more content but can go scroll up as well. To save you some time (if not interested in reading whole script) this is what i am checking (code near the end of the script):



 $(window).scrollTop() + $(window).height() > $("#load_data").height()


Which is in Vanilla JS from my understanding this:



 window.innerHeight + window.scrollY > document.querySelector('#load_div').offsetHeight


scrollTop() and scrollY should return(or set) value of pixels from the top of window(in this case) scrolled. Then we add inner height of window. Here is what i don't understand then, Shouldn't those 2 always be bigger then the divs height? So that explains why wherever i scroll it triggers. But how to set this up so it only triggers when i am at the bottom or near it? Here is full code (without php response part which only returns paragraph that got data from database):



            <div id="load_data"></div>
<div id="load_data_message"></div>

</div>

<script>
$(document).ready(function(){
var limit = 8;
var start = 0;
var action = 'inactive';
function load_data(limit, start){

$.ajax({
url: 'infinitload.php',
method: 'POST',
data: {limit: limit, start: start},
cache: false,
success: function(data){
$('#load_data').append(data); //Appending data;

if(data == ''){
$('#load_data_message').html('<button>No results found</button>');
action = 'active';//if
}else{
$('#load_data_message').html('<button>Loading Content</button>');
action = 'inactive';
}//else

}//success
})//ajax
}//load_data()

//Loading for the first time
if(action == 'inactive'){
action = 'active';
load_data(limit, start);
}

$(window).scroll(function(){
if( $(window).scrollTop() + $(window).height() > $("#load_data").height() && action == 'inactive'){
$('button:contains("Loading Content")').toggle();
action = 'active';
start = start + limit;
setTimeout(function(){
load_data(limit, start);
}, 1500);
}
});
});//dom ready

</script>


</body>


UPDATE:
I tried code like this http://jsfiddle.net/nick_craver/gWD66/
which totally makes sense and it works in example there but not in my case. Confused. It's probably something else leading to my problem so i ll try to look into my code again. Thanks for all replies.







javascript jquery ajax window height






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 22:21







Vukasin Sevo

















asked Nov 20 '18 at 21:32









Vukasin SevoVukasin Sevo

124




124













  • Does the entire page scroll, or does a subsection of the page scroll?

    – Taplar
    Nov 20 '18 at 21:42











  • Thanks for the reply. Entire page

    – Vukasin Sevo
    Nov 20 '18 at 21:48











  • One approach I have done in the past is to throw an empty span after the container that the loaded content will be appended to. Then it is a pretty simple check to see if the offsetTop of that element is >= the scroll distance of the window. If it's >=, the user has scrolled enough so that the element is in their view, indicating more content needs to be added.

    – Taplar
    Nov 20 '18 at 21:51



















  • Does the entire page scroll, or does a subsection of the page scroll?

    – Taplar
    Nov 20 '18 at 21:42











  • Thanks for the reply. Entire page

    – Vukasin Sevo
    Nov 20 '18 at 21:48











  • One approach I have done in the past is to throw an empty span after the container that the loaded content will be appended to. Then it is a pretty simple check to see if the offsetTop of that element is >= the scroll distance of the window. If it's >=, the user has scrolled enough so that the element is in their view, indicating more content needs to be added.

    – Taplar
    Nov 20 '18 at 21:51

















Does the entire page scroll, or does a subsection of the page scroll?

– Taplar
Nov 20 '18 at 21:42





Does the entire page scroll, or does a subsection of the page scroll?

– Taplar
Nov 20 '18 at 21:42













Thanks for the reply. Entire page

– Vukasin Sevo
Nov 20 '18 at 21:48





Thanks for the reply. Entire page

– Vukasin Sevo
Nov 20 '18 at 21:48













One approach I have done in the past is to throw an empty span after the container that the loaded content will be appended to. Then it is a pretty simple check to see if the offsetTop of that element is >= the scroll distance of the window. If it's >=, the user has scrolled enough so that the element is in their view, indicating more content needs to be added.

– Taplar
Nov 20 '18 at 21:51





One approach I have done in the past is to throw an empty span after the container that the loaded content will be appended to. Then it is a pretty simple check to see if the offsetTop of that element is >= the scroll distance of the window. If it's >=, the user has scrolled enough so that the element is in their view, indicating more content needs to be added.

– Taplar
Nov 20 '18 at 21:51












2 Answers
2






active

oldest

votes


















0














Try to replace



$("#load_data").height()


with



$("#load_data").offset().top





share|improve this answer


























  • You should do something with action == 'inactive' as well

    – Denis Kovzelyuk
    Nov 20 '18 at 22:12













  • $("#load_data").height() returns height of that element not offset

    – Denis Kovzelyuk
    Nov 20 '18 at 22:37



















0














Problem is finally fixed. I forgot html DOCTYPE thing. Never got annoyed more :)






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401879%2fjavascript-loading-more-content-with-ajax-problem%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try to replace



    $("#load_data").height()


    with



    $("#load_data").offset().top





    share|improve this answer


























    • You should do something with action == 'inactive' as well

      – Denis Kovzelyuk
      Nov 20 '18 at 22:12













    • $("#load_data").height() returns height of that element not offset

      – Denis Kovzelyuk
      Nov 20 '18 at 22:37
















    0














    Try to replace



    $("#load_data").height()


    with



    $("#load_data").offset().top





    share|improve this answer


























    • You should do something with action == 'inactive' as well

      – Denis Kovzelyuk
      Nov 20 '18 at 22:12













    • $("#load_data").height() returns height of that element not offset

      – Denis Kovzelyuk
      Nov 20 '18 at 22:37














    0












    0








    0







    Try to replace



    $("#load_data").height()


    with



    $("#load_data").offset().top





    share|improve this answer















    Try to replace



    $("#load_data").height()


    with



    $("#load_data").offset().top






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 22:07

























    answered Nov 20 '18 at 21:56









    Denis KovzelyukDenis Kovzelyuk

    716




    716













    • You should do something with action == 'inactive' as well

      – Denis Kovzelyuk
      Nov 20 '18 at 22:12













    • $("#load_data").height() returns height of that element not offset

      – Denis Kovzelyuk
      Nov 20 '18 at 22:37



















    • You should do something with action == 'inactive' as well

      – Denis Kovzelyuk
      Nov 20 '18 at 22:12













    • $("#load_data").height() returns height of that element not offset

      – Denis Kovzelyuk
      Nov 20 '18 at 22:37

















    You should do something with action == 'inactive' as well

    – Denis Kovzelyuk
    Nov 20 '18 at 22:12







    You should do something with action == 'inactive' as well

    – Denis Kovzelyuk
    Nov 20 '18 at 22:12















    $("#load_data").height() returns height of that element not offset

    – Denis Kovzelyuk
    Nov 20 '18 at 22:37





    $("#load_data").height() returns height of that element not offset

    – Denis Kovzelyuk
    Nov 20 '18 at 22:37













    0














    Problem is finally fixed. I forgot html DOCTYPE thing. Never got annoyed more :)






    share|improve this answer




























      0














      Problem is finally fixed. I forgot html DOCTYPE thing. Never got annoyed more :)






      share|improve this answer


























        0












        0








        0







        Problem is finally fixed. I forgot html DOCTYPE thing. Never got annoyed more :)






        share|improve this answer













        Problem is finally fixed. I forgot html DOCTYPE thing. Never got annoyed more :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 23:21









        Vukasin SevoVukasin Sevo

        124




        124






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401879%2fjavascript-loading-more-content-with-ajax-problem%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents