Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: $(...).owlCarousel is not a function

I am having troubles adding this carousel to my prestashop template.

It returns me the following error:

TypeError: $(...).owlCarousel is not a function navigation : true

And the code using to initialize it is this one

$(document).ready(function() {
  $("#owl-demo").owlCarousel({
    navigation : true
  });
});

I am trying to solve it, but seems imposible, since on an empty html page it works but not when I use it on the Prestashop.

Any clue?

Thanks.

like image 244
Xavi Alsina Avatar asked Feb 09 '14 15:02

Xavi Alsina


4 Answers

Add owl.carousel.min.js file in your home page and before the file in which you are using add the following code:

$("#owl-demo").owlCarousel({
    navigation : true
  });

Then only it will work.

like image 59
Manoj Verma Avatar answered Oct 20 '22 22:10

Manoj Verma


You will get this error if the jquery file is loading after the owl carousel file.

(Make sure your reference to jquery is above the owl carousel reference js file)

like image 39
Tom Avatar answered Oct 20 '22 22:10

Tom


If JavaScript files loading is affected by some latency, you can check if function is defined before call it.

Check with jQuery.isFunction

if($.isFunction('owlCarousel')){
  $("#owl-demo").owlCarousel({
    navigation : true
  });
}

Check with JavaScript typeof operator

if(typeof owlCarousel === 'function') { 
  $("#owl-demo").owlCarousel({
    navigation : true
  });
}
like image 6
Nolwennig Avatar answered Oct 20 '22 21:10

Nolwennig


The reason sometime html executed inline script before external script loaded perfectly. I get solution by this way . I just added defer attribute to my owl.carouselsource calling like ..

<script defer src="plugins/OwlCarousel2.3/owl.carousel.min.js"></script>

Documentation about defer attribute --> att_script_defer-link

like image 3
MD Ashik Avatar answered Oct 20 '22 23:10

MD Ashik