0

I am trying to use infinite ajax scroll plugin in my project. I just followed the official website and include the necessary javascript files. Following is my code:

<div class="row">
   <div class="col-lg-4">
       <div class="bootstrap-card">
       <img src="{% static 'images/1.jpg' %}" class="img-responsive img-rounded" alt="card imag">
       <div class="overlay">
       <a class="info" href="#">View Details</a>
       </div>
      </div>
   </div>

    <div class="col-lg-4">
        <div class="bootstrap-card">
        <img src="{% static 'images/1.jpg' %}" class="img-responsive img-rounded" alt="card imag">
        <div class="overlay">
        <a class="info" href="#">View Details</a>
        </div>
        </div>
    </div>

    <div class="col-lg-4">
       <div class="bootstrap-card">
       <img src="{% static 'images/1.jpg' %}" class="img-responsive img-rounded" alt="card imag">
       <div class="overlay">
       <a class="info" href="#">View Details</a>
       </div>
       </div>
    </div>
</div>

<script src="{% static 'hw1/js/callback.js' %}"></script>
<script src="{% static 'hw1/js/jquery-ias.min.js' %}"></script>

<div id="pagination">
    <a href="page2.html" class="next">next</a>
</div>

<script>
    $(document).ready(function() {
      var ias = jQuery.ias({
        container: '.row',
        item: '.col-lg-4',
        pagination: '#pagination',
        next: '.next',
        delay: 1250
      });
    });

    ias.extension(new IASSpinnerExtension());
    ias.extension(new IASTriggerExtension({offset: 2}));
    ias.extension(new IASNoneLeftExtension({text: "You reached the end"}));
</script>

So here page2.html is another page in and it does exist.

So does anybody know why the error message is:

(index):244 Uncaught ReferenceError: ias is not defined(…)(anonymous function) @ (index):244 jquery-3.1.1.min.js:2 jQuery.Deferred exception: jQuery.ias is not a function TypeError: jQuery.ias is not a function at HTMLDocument. (http://localhost:8000/:235:24) at j (http://localhost:8000/static/hw1/js/jquery-3.1.1.min.js:2:29948) at k (http://localhost:8000/static/hw1/js/jquery-3.1.1.min.js:2:30262) undefined

Soviut
  • 79,529
  • 41
  • 166
  • 227
user2970089
  • 215
  • 3
  • 14

1 Answers1

0

You have a scope issue. You define var ias inside the jQuery ready callback but are trying to reference the ias variable outside of that callback. Outside of that callback, the ias variable doesn't exist. Additionally, because the callback is asynchronous, the callback won't be called until the DOM is fully loaded. This means your calls to ias.extension() are happening before the page has even had a chance to load, which is why the ready callback exists in the first place.

To fix this, put your calls to ias.extension() inside the callback as well so they they're all in the same scope where both jquery and ias have been initialized.

$(document).ready(function() {
  var ias = jQuery.ias({
    container: '.row',
    item: '.col-lg-4',
    pagination: '#pagination',
    next: '.next',
    delay: 1250
  });

  ias.extension(new IASSpinnerExtension());
  ias.extension(new IASTriggerExtension({offset: 2}));
  ias.extension(new IASNoneLeftExtension({text: "You reached the end"}));
});
Soviut
  • 79,529
  • 41
  • 166
  • 227
  • Hi Thanks for your explanation. However, I still get the same error. It seems it always looking for the function defined in jquery-3.1.1.min.js. The error message is:jquery-3.1.1.min.js:2 jQuery.Deferred exception: jQuery.ias is not a function TypeError: jQuery.ias is not a function at HTMLDocument. (http://localhost:8000/:235:24) at j (http://localhost:8000/static/hw1/js/jquery-3.1.1.min.js:2:29948) at k (http://localhost:8000/static/hw1/js/jquery-3.1.1.min.js:2:30262) – user2970089 Nov 14 '16 at 05:46
  • @user2970089 does http://localhost:8000/static/hw1/js/jquery-ias.min.js return javascript or does it give an error? If it gives an error, your static files are probably not set up correctly or you have a typo in your urls. – Soviut Nov 14 '16 at 05:54
  • It return me the javascript. I checked there is no typo and I also use the similar things to add other javascript files. So I am confused why this function is not able to be found... – user2970089 Nov 14 '16 at 06:10
  • Another error message it shows is: jquery-ias.min.js:13 Uncaught ReferenceError: jQuery is not defined at http://localhost:8000/static/hw1/js/jquery-ias.min.js:13:6783(anonymous function) @ jquery-ias.min.js:13 – user2970089 Nov 14 '16 at 06:13
  • It seems like the plugin has a bug, then. Go report it to them on their github page. – Soviut Nov 14 '16 at 06:24