23

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.

Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
Xavi Alsina
  • 643
  • 1
  • 8
  • 24

13 Answers13

17

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.

Ram
  • 3,092
  • 10
  • 40
  • 56
Manoj Verma
  • 518
  • 1
  • 5
  • 12
14

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)

Tom
  • 273
  • 4
  • 8
7

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
  });
}
Nolwennig
  • 1,613
  • 24
  • 29
7

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.carousel source calling like ..

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

Documentation about defer attribute --> att_script_defer-link

MD Ashik
  • 9,117
  • 10
  • 52
  • 59
1

Try to use {literal} {/literal} tags. It's usually reccomanded to put javascript inside those tags in .tpl files (smarty) . Javascript might work without the tags but can sometimes return a error ( like in your case )

BR's

user2831723
  • 832
  • 12
  • 25
  • I also tried this but keeps without work. It just keeps me returning me the same error. – Xavi Alsina Feb 09 '14 at 22:52
  • how are you pulling the js in to the file ? Are you using pretsatshop determined pathways in .php (whatever core/module file is displaying ur .tpl file) file ? Example is here: $this->context->controller->addJS(($this->_path).'views/js/YOUR.js'); – user2831723 Feb 10 '14 at 07:36
  • I am just adding the script between – Xavi Alsina Feb 10 '14 at 08:21
  • Are you going to use that script on every single page ? And even then you should still add it like that ( if you are using it in within a module , for example ) In your case prestashop will load the script every single time even when it's not being used. – user2831723 Feb 10 '14 at 10:00
  • I just want to use it in the home page (otherwise, thanks for warning me about it will be displayed on every single page). So i will have to add the script on the prestashop .php path and add the script? And what about the begining of the function? On the php file also? Or I'll have to make another JS file with the function? – Xavi Alsina Feb 10 '14 at 12:00
  • Only reason I can think of is that your link to the javascript is not correct as the function can't execute. To display something ONLY on homepage without some module or core files edit use this : {if $page_name == 'index'} {/if} – user2831723 Feb 11 '14 at 10:11
  • Hey user2831723, i will give a try later. Otherwise I am asking the same question on prestashop forums, and with the help of moderators, we checked the code and the path is correct – Xavi Alsina Feb 11 '14 at 11:11
  • I'll try to do some testing when I get some time. I'll get back to you :) – user2831723 Feb 11 '14 at 11:23
1

I got fixed by checking if the selector exist or not. As this was the issue on my website I had added code in footer for a single page but it was throwing error or other pages where that selector don't exist.

$(document).ready(function() {
    var owl = $('.servicesCarosal');
            
    //console.log(owl);

    if (owl.length) { 
        owl.owlCarousel({

        });
    } 
});
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
1

First, add Owl Carousel Css and Owl Carousel Theme Default Css files.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" integrity="sha512-UTNP5BXLIptsaj5WdKFrkFov94lDx+eBvbKyoe1YAfjeRPC+gT5kyZ10kOHCfNZqEui1sxmqvodNUx3KbuYI/A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css" integrity="sha512-OTcub78R3msOCtY3Tc6FzeDJ8N9qvQn1Ph49ou13xgA9VsH9+LRxoFU6EqLhW4+PKRfU+/HReXmSZXHEkpYoOA==" crossorigin="anonymous" referrerpolicy="no-referrer" />

Then add Jquery and Owl Carousel Js files.

Make sure to put Jquery before Owl Carousel Js.

Like this:

<!-- put jquery before owl carousel -->
<!-- jquery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

<!-- owl carousel -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js" integrity="sha512-bPs7Ae6pVvhOSiIcyUClR7/q2OAsRiovw4vAkX+zJbw3ShAeeqezq50RIIcIURq7Oa20rW2n2q+fyXBNcU9lrw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Mahbub Ul Islam
  • 773
  • 8
  • 15
1

Add jquery.min.js and owl.carousel.min.js files in your home page and add the following code:

$(document).ready(function() {
  var owl = $("#owl-demo");
  if(owl.length){
    owl.owlCarousel({
      navigation : true
    });
  }
});
Simo OM
  • 139
  • 6
1

These might be helpful. Follow the steps

  1. Import/link the owlCarousel.js file
  2. Link owlCarousel.js file after jQuery.js file.
  3. Hard reload: ctrl + shift + R

Still facing problem?

It might be for this reason:

Your $(".owl-carousel").owlCarousel() function is executing before the loading of JS files (owlCarousel.js,jQuery.js)

Most of the case you can solve this issue by using this tricks & increasing timeout.

$(document).ready(function() {
    setTimeout(function(){
        $(".owl-carousel").owlCarousel()
    },
    2000);
});
MD TAREK HOSSEN
  • 129
  • 2
  • 11
  • I'm running Rails... so it's pretty hard to get the order of scripts to load right. THIS is the only answer that worked for me. – mystic cola May 22 '22 at 15:20
0

I had the same problem. Just add the js file right above your function

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
<!--DO NOT ENTER ANY EXTERNAL LINK IN BETWEEN-->
<script type="text/javascript">
$(document).ready(function() {
    $('.owl-carousel').owlCarousel({
        loop: true,
    });
});
</script>
0

In my case, where I am using Webpack to bundle my assets, I needed to follow this section on the owl's page on GitHub.

The section states that you should add JQuery like so:

webpack.config.js

const webpack = require('webpack');

//...
plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery'
    }),
],
//...

index.js

import 'owl.carousel/dist/assets/owl.carousel.css';
import 'owl.carousel';
Weam Adel
  • 151
  • 1
  • 11
-1

Add Your Jquery file and owl.js file to header section

Gowthaman D
  • 574
  • 7
  • 19
-1

For me, adding itemclass to carousel-item

-result is below-

<div class="item carousel-item" style="margin: auto;">

solved problem.