jquery 3.0 url.indexOf error
Solution 1
Update all your code that calls load
function like,
$(window).load(function() { ... });
To
$(window).on('load', function() { ... });
jquery.js:9612 Uncaught TypeError: url.indexOf is not a function
This error message comes from jQuery.fn.load
function.
I've come across the same issue on my application. After some digging, I found this statement in jQuery blog,
.load, .unload, and .error, deprecated since jQuery 1.8, are no more. Use .on() to register listeners.
I simply just change how my jQuery objects call the load
function like above. And everything works as expected.
Solution 2
Better approach may be a polyfill like this
jQuery.fn.load = function(callback){ $(window).on("load", callback) };
With this you can leave the legacy code untouched. If you use webpack be sure to use script-loader.
Solution 3
Jquery 3.0 has some breaking changes that remove certain methods due to conflicts. Your error is most likely due to one of these changes such as the removal of the .load() event.
Read more in the jQuery Core 3.0 Upgrade Guide
To fix this you either need to rewrite the code to be compatible with Jquery 3.0 or else you can use the JQuery Migrate plugin which restores the deprecated and/or removed APIs and behaviours.
Solution 4
I came across the same error after updating to the latest version of JQuery. Therefore I updated the jquery file I was working on, as stated in a previous answer, so it said .on("load")
instead of .load()
.
This fix isn't very stable and sometimes it didn't work for me. Therefore to fix this issue you should update your code from:
.load();
to
.trigger("load");
I got this fix from the following source: https://github.com/stevenwanderski/bxslider-4/pull/1024
Related videos on Youtube
Kamrul
Updated on March 17, 2022Comments
-
Kamrul over 2 years
I am getting following error from jQuery once it has been updated to
v3.0.0
.jquery.js:9612 Uncaught TypeError: url.indexOf is not a function
Any Idea why?
-
Nalin Aggarwal about 8 yearscheck this, may be it resolves your problem Uncaught TypeError: Undefined is not a function on indexOf
-
Kamrul about 8 yearsit's the new version of jQuery firing the error. Old version is fine.
-
Feek about 8 yearsim running into this right now too :0
-
Mladen Janjetovic almost 8 yearsPlease declare the answer. I think it's obvious we have one
-
-
James McMahon about 8 yearsIt's crazy they don't have a big deprecated warning on this page in their docs, api.jquery.com/load.
-
samuel.molinski almost 8 yearsI found this the be the exact problem I was having when bootstrapping zurbs foundation into aurelia
-
aruno over 7 yearskind of ironic that the only place I actually was still doing this was in error handling code that then broke itself :-/
-
rgfx about 7 yearsThanks so much. I still got an error, James suggested you use .trigger this worked. jQuery.fn.load = function (callback) {$(window).trigger("load", callback); };
-
KCarnaille almost 6 yearsThanks for this, perfect answer
-
Mateusz over 5 yearsAmazing solution when it's caused by third party JS. Another plus is that it doesn't require a downgrade, which will cause vulnerability (in any jquery version below 3!).
-
Ntiyiso Rikhotso about 5 yearsThis answer is king
-
prem30488 over 4 yearsI used it in
document.ready
function and it is working like a charm. -
Kamlesh over 4 yearsNot working dear, getting same error. Code is as <script type="text/javascript"> $(window).on('load', function() { $(document).on('click', '[data-toggle="lightbox"]', function(event) { event.preventDefault(); $(this).ekkoLightbox(); }); }); </script>
-
Kamlesh over 4 yearsi have also added code in document.ready but still not working. any suggestion?
-
Diego Betto over 3 yearsmy 2cents:
jQuery.fn.load = function(callback){ jQuery(window).on("load", callback) };
, usejQuery
insted of$
if not working (like in many WordPress...)