367

I've created a new Foundation 5 project through bash, with foundation new my-project. When I open the index.html file in Chrome an Uncaught TypeError: a.indexOf is not a function error is shown in the console, originating in jquery.min.js:4.

I created the project following the steps on the foundation site, but I can't seem to get rid of this error. Foundation and jQuery look like they are included and linked up correctly in the index.html file, and the linked app.js file is including $(document).foundation();

Does anyone know what is causing this error? and what a solution might be?

Console error message screenshot

Daniel Llano
  • 11,568
  • 1
  • 9
  • 7
FreddieE
  • 3,793
  • 2
  • 10
  • 7
  • 2
    The top-voted answer below should be accepted! It explains the problem, links to POD, and gives a clear solution, in a short post. – HoldOffHunger Jan 21 '21 at 16:26

8 Answers8

1153

This error might be caused by the jQuery event-aliases like .load(), .unload() or .error() that all are deprecated since jQuery 1.8. Lookup for these aliases in your code and replace them with the .on() method instead. For example, replace the following deprecated excerpt:

$(window).load(function(){...});

with the following:

$(window).on('load', function(){ ...});
someOne
  • 1,975
  • 2
  • 14
  • 20
Daniel Llano
  • 11,568
  • 1
  • 9
  • 7
77

Please add below jQuery Migrate Plugin

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.4.1.min.js"></script>
  • 3
    I confirm that this has fixed the issue. – Morad Hamdy May 29 '20 at 20:40
  • thats fixed my issue, Thanks – Ramazan Zülkarneyn Nov 26 '20 at 09:34
  • 2
    How can this possibly work? Installing jQuery migrate is for code migrations, and installing jQuery-specific versions address version issues. None of these could solve this problem long-term. – HoldOffHunger Jan 26 '21 at 03:39
  • For me, this could be a temporary workaround while a 3rd party plugin you are using still has an incompatible jquery version with your own website. Until the plugin updates it, this seems to be a viable solution. I understand that this is not the migrate script's purpose yet still it can be a workaround. – reikyoushin May 11 '21 at 08:15
39

This error is often caused by incompatible jQuery versions. I encountered the same error with a foundation 6 repository. My repository was using jQuery 3, but foundation requires an earlier version. I then changed it and it worked.

If you look at the version of jQuery required by the foundation 5 dependencies it states "jquery": "~2.1.0".

Can you confirm that you are loading the correct version of jQuery?

I hope this helps.

Hari Harker
  • 702
  • 1
  • 12
  • 29
shaune
  • 1,020
  • 8
  • 12
  • Foundation 5.5.1 states `jQuery >= 2.1.0`: https://github.com/foundation/foundation-sites/blob/a89eb88ab344b6639e0dd7df63a97324d97a89ad/bower.json – Daniel Jan 27 '21 at 19:30
21

I faced this issue too. I was using jquery.poptrox.min.js for image popping and zooming and I received an error which said:

“Uncaught TypeError: a.indexOf is not a function” error.

This is because indexOf was not supported in 3.3.1/jquery.min.js so a simple fix to this is to change it to an old version 2.1.0/jquery.min.js.

This fixed it for me.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Harshit Pant
  • 1,032
  • 11
  • 14
15

One of the possible reasons is when you load jQuery TWICE ,like:

<script src='..../jquery.js'></script>
....
....
....
....
....
<script src='......./jquery.js'></script>

So, check your source code and remove duplicate jQuery load.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 3
    It happens with wordpress, loading its own version, if you are also loading a jquery version on your side. – Olou Dec 28 '18 at 14:15
3

I'm using jQuery 3.3.1 and I received the same error, in my case, the URL was an Object vs a string.

What happened was, that I took URL = window.location - which returned an object. Once I've changed it into window.location.href - it worked w/o the e.indexOf error.

Ricky Levi
  • 7,298
  • 1
  • 57
  • 65
0

I solved this by installing the correct version of Jquery that my project required using npm

Ntiyiso Rikhotso
  • 644
  • 6
  • 15
0

It's seems to be funny but no one take consideration of the following.

  1. Discover if you are having a library that requires an old version of jQuery. If you can't discover the version, You can do it commenting and uncommenting every script line until you find it.
  2. Open the library and find the author.
  3. Search in google for an update of the library. 90% you will find it.
  4. Update the reference of your obsolete library that requires and old version of jQuery.

IN ANY CASE NEVER DOWNGRADE YOUR JQUERY VERSION

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116