"Uncaught TypeError: a.indexOf is not a function" error when opening new foundation project
Asked Answered
R

8

373

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

Romanaromanas answered 10/8, 2016 at 11:8 Comment(1)
The top-voted answer below should be accepted! It explains the problem, links to POD, and gives a clear solution, in a short post.Ogive
R
1170

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(){ ...});
Rustice answered 26/10, 2016 at 2:25 Comment(10)
I didn't have it with 2.2.1 @User, but as soon as I switched to 3.3.1 I had to make this change.Maggard
@Crine $(function() {}); is for document ready, which is not the same thing as the window load event. Also, doc ready often fires before window load.Expiate
Hello man, could you tell me why? before it was working with version 1.12.3 and it stopped working when I migrated to 3.4.1Valiancy
Not Working Dear, i am using jquey 3.3.1 version. Thanks.Cassiecassil
@Kamlesh- if the solution for this issue doesn't solve your problem then it you'll need to post a new question, including examples specific to your issue and what you've tried.Potvaliant
Worked like a charm. $(window).on('load', function(){Trenatrenail
This was the issue, it can also be fixed by applying jquery-migrate-1.4.1.jsFiddler
@Romanaromanas or moderator, Please check this as the correct answerExoskeleton
this is the correct answer and must be accepted. Thanks, Also downgrading is not a solution as suggested by some others.Berthold
I have used a code from a new sticker. This was build on jQuery v1.10.2 mine is jQuery v3.6.0. So I was facing the error. Thank youSold
T
81

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>
Tahiti answered 4/4, 2019 at 10:45 Comment(4)
I confirm that this has fixed the issue.Manatarms
thats fixed my issue, ThanksKeister
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.Ogive
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.Heteronomous
R
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.

Rattail answered 17/8, 2016 at 1:46 Comment(1)
Foundation 5.5.1 states jQuery >= 2.1.0: github.com/foundation/foundation-sites/blob/…Coprophilia
E
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.

Elma answered 2/4, 2018 at 14:20 Comment(6)
I moved to 3.2.1 to 2.1.0 and it worked. This worked for me.Miksen
Welcome ! @MiksenElma
never ever back down to versions as it may result in majorly security issues.Marylnmarylou
If that is the case: you can replace the below code $(window).load(function(){...}); with the following: $(window).on('load', function(){ ...}); that would also work! :)Elma
Its not good to revert back to older version.Suspend
DO NOT DO THIS. Search for an update of your library or fix your library.Racial
S
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.

Seto answered 26/10, 2017 at 21:30 Comment(1)
It happens with wordpress, loading its own version, if you are also loading a jquery version on your side.Kerley
D
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.

Deuno answered 3/3, 2019 at 12:15 Comment(0)
E
0

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

Enchase answered 18/6, 2019 at 9:50 Comment(0)
R
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

Racial answered 18/6, 2021 at 15:45 Comment(3)
You are right, it my case there was library requires older version of jQueryCatechize
This is just general advice but doesn't answer the question.Gravy
@Colin'tHart in my case, resolve the version was the answer to the same problem.Racial

© 2022 - 2024 — McMap. All rights reserved.