jquery 3.0 url.indexOf error

160,927

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

Share:
160,927

Related videos on Youtube

Kamrul
Author by

Kamrul

Updated on March 17, 2022

Comments

  • Kamrul
    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
      Nalin Aggarwal about 8 years
      check this, may be it resolves your problem Uncaught TypeError: Undefined is not a function on indexOf
    • Kamrul
      Kamrul about 8 years
      it's the new version of jQuery firing the error. Old version is fine.
    • Feek
      Feek about 8 years
      im running into this right now too :0
    • Mladen Janjetovic
      Mladen Janjetovic almost 8 years
      Please declare the answer. I think it's obvious we have one
  • James McMahon
    James McMahon about 8 years
    It's crazy they don't have a big deprecated warning on this page in their docs, api.jquery.com/load.
  • samuel.molinski
    samuel.molinski almost 8 years
    I found this the be the exact problem I was having when bootstrapping zurbs foundation into aurelia
  • aruno
    aruno over 7 years
    kind of ironic that the only place I actually was still doing this was in error handling code that then broke itself :-/
  • rgfx
    rgfx about 7 years
    Thanks so much. I still got an error, James suggested you use .trigger this worked. jQuery.fn.load = function (callback) {$(window).trigger("load", callback); };
  • KCarnaille
    KCarnaille almost 6 years
    Thanks for this, perfect answer
  • Mateusz
    Mateusz over 5 years
    Amazing 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
    Ntiyiso Rikhotso about 5 years
    This answer is king
  • prem30488
    prem30488 over 4 years
    I used it in document.ready function and it is working like a charm.
  • Kamlesh
    Kamlesh over 4 years
    Not 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
    Kamlesh over 4 years
    i have also added code in document.ready but still not working. any suggestion?
  • Diego Betto
    Diego Betto over 3 years
    my 2cents: jQuery.fn.load = function(callback){ jQuery(window).on("load", callback) };, use jQuery insted of $ if not working (like in many WordPress...)