jquery 3.0 url.indexOf error

后端 未结 5 771
臣服心动
臣服心动 2020-11-27 10:24

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

相关标签:
5条回答
  • 2020-11-27 11:04

    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.

    0 讨论(0)
  • 2020-11-27 11:08

    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.

    0 讨论(0)
  • 2020-11-27 11:08

    @choz answer is the correct way. If you have many usages and want to make sure it works everywhere without changes you can add these small migration-snippet:

    /* Migration jQuery from 1.8 to 3.x */
    jQuery.fn.load = function (callback) {
        var el = $(this);
    
        el.on('load', callback);
    
        return el;
    };
    

    In this case you got no erros on other nodes e.g. on $image like in @Korsmakolnikov answer!

    const $image = $('img.image').load(function() {
      $(this).doSomething();
    });
    
    $image.doSomethingElseWithTheImage();
    
    0 讨论(0)
  • 2020-11-27 11:16

    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

    0 讨论(0)
  • 2020-11-27 11:19

    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.

    0 讨论(0)
提交回复
热议问题