0

I am working on this quite strange issue for several days now, and I really don't understand why that happens.

Basically, the environment is WordPress and I am including two js files:

vendor.js -> generated by gulp and all it does is concatenating bower libs together

myScript.js -> my code

What I know (with proof):

  • The vendor file is included before my code (checked from the console)
  • The plugin I am using is included in vendor.js (opened the file in a new tab in chrome)
  • Both files are loaded correctly (i printed some debug console log and they are in the console)

BUT even if everything looks fine to me, the plugin is still undefined for whatever reason. The plugin I am using is this one: https://github.com/idiot/unslider

and the error i'm getting in the console is this:

Uncaught ReferenceError: unslider is not defined

This is the part of my code where I call and need this plugin:

jQuery(document).ready(function($) {
  console.log("unslider is ", unslider);
  $('#topBannerSlider').unslider({
    autoplay: true,
    delay: 10000,
    keys: true,
    nav: true,
    arrows: {
      prev: '<a class="unslider-arrow prev fa fa-arrow-circle-left"></a>',
      next: '<a class="unslider-arrow next fa fa-arrow-circle-right"></a>'
    }
  });

  $('#homePageTestimonialsCarousel').unslider({
    autoplay: true,
    delay: 20000,
    keys: true,
    nav: true,
    arrows: false
  });
}

and it does the same if I try to load it before jQuery(document).ready() event, like so:

console.log("unslider is ", unslider);
jQuery(document).ready(function($) {
  $('#topBannerSlider').unslider({
    autoplay: true,
    delay: 10000,
    keys: true,
    nav: true,
    arrows: {
      prev: '<a class="unslider-arrow prev fa fa-arrow-circle-left"></a>',
      next: '<a class="unslider-arrow next fa fa-arrow-circle-right"></a>'
    }
  });

  $('#homePageTestimonialsCarousel').unslider({
    autoplay: true,
    delay: 20000,
    keys: true,
    nav: true,
    arrows: false
  });
}

Do you have any suggestion? what else can I try? what else can it be? I am really stuck and any help would be really appreciated. Thanks a lot

7
  • check the order those scripts are being loaded, first include jQuery, then unslider, then your js code. Commented Oct 15, 2017 at 9:55
  • Hi, I checked it, everything is in the proper order..that's why it's really strange to me Commented Oct 15, 2017 at 9:56
  • in the code samples they load unslider.js after html is defined, are you doing the same? Commented Oct 15, 2017 at 9:58
  • Do you have the async or defer attribute set on the <script> tag which is loading the library? Please also check the console for errors, as there should be some clue as to why your code isn't working Commented Oct 15, 2017 at 9:58
  • I have copied your code and it's working fine. The only thing was you had a ) missing at the end of your code. Not sure if this is unrelated issue Commented Oct 15, 2017 at 10:23

1 Answer 1

3

If I get this right you say WordPress, so try

jQuery insted of $

I have this problem before and this fix it.

WordPress, PHP, JQuery and $ don't mix ;).

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

2 Comments

I don't believe it, but this actually solved the issue! Really strange, but hey, thanks man you saved my weekend :)
anytime dude ;)

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.