jquery 3.0 url.indexOf 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37738732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:07:50  来源:igfitidea点击:

jquery 3.0 url.indexOf error

jqueryjquery-3

提问by Kamrul

I am getting following error from jQuery once it has been updated to v3.0.0.

将 jQuery 更新为v3.0.0.

jquery.js:9612 Uncaught TypeError: url.indexOf is not a function

jquery.js:9612 Uncaught TypeError: url.indexOf is not a function

Any Idea why?

任何想法为什么?

回答by vadi taslim

Updateall your code that calls loadfunction like,

更新所有调用load函数的代码,例如,

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

To

$(window).on('load', function() { ... });


jquery.js:9612 Uncaught TypeError: url.indexOf is not a function

jquery.js:9612 Uncaught TypeError: url.indexOf 不是函数

This error message comes from jQuery.fn.loadfunction.

此错误消息来自jQuery.fn.load函数。

I've come across the same issue on my application. After some digging, I found this statement in jQuery blog,

我在我的应用程序中遇到了同样的问题。经过一番挖掘,我在jQuery 博客中找到了这个语句,

.load, .unload, and .error, deprecated since jQuery 1.8, are no more. Use .on() to register listeners.

.load、.unload 和 .error,自 jQuery 1.8 起已弃用,不再. 使用 .on() 注册监听器。

I simply just change how my jQuery objects call the loadfunction like above. And everything works as expected.

我只是简单地改变我的 jQuery 对象如何调用load上面的函数。一切都按预期进行。

回答by Korsmakolnikov

Better approach may be a polyfill like this

更好的方法可能是这样的 polyfill

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.

有了这个,您可以保持遗留代码不变。如果你使用 webpack,一定要使用 script-loader。

回答by F3CP

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.

Jquery 3.0 有一些重大更改,删除了由于冲突而导致的某些方法。您的错误很可能是由于这些更改之一造成的,例如删除了 .load() 事件。

Read more in the jQuery Core 3.0 Upgrade Guide

jQuery Core 3.0 升级指南中阅读更多内容

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 pluginwhich restores the deprecated and/or removed APIs and behaviours.

要解决此问题,您需要重写代码以与 Jquery 3.0 兼容,否则您可以使用JQuery Migrate 插件来恢复已弃用和/或删除的 API 和行为。

回答by James

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().

更新到最新版本的 JQuery 后,我遇到了同样的错误。因此,我更新了我正在处理的 jquery 文件,如之前的答案所述,所以它说.on("load")而不是.load().

This fix isn't very stable and sometimes it didn't work for me. Therefore to fix this issue you should updateyour code from:

此修复程序不是很稳定,有时对我不起作用。因此,要解决此问题,您应该从以下位置更新代码:

    .load();

to

    .trigger("load");

I got this fix from the following source: https://github.com/stevenwanderski/bxslider-4/pull/1024

我从以下来源得到了这个修复:https: //github.com/stevenwanderski/bxslider-4/pull/1024