jQuery 3.0: Fix url.indexOf is Not a Function Error

jQuery 3.0 is out, and if you started working with it and plan to work with this, then you may encounter the error “Uncaught TypeError: url.indexOf is not a function”. This error is related to jQuery 3.0 and it is occurring due to one of the change made to jQuery 3.0.

First, let’s see how this error occurs.Following is the syntax that we all use for attaching loadevent with window’s object.

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

Quite familiar. Isn’t it? But if you use the same code with jQuery 3.0, then you will get the error “Uncaught TypeError: url.indexOf is not a function”. This is because .load, .unload, and .error, deprecated since jQuery 1.8, and are no more. These events were deprecated since jQuery 1.8 but were still available to use for a while, but with jQuery 3.0, they have been removed, and when you try to use them you'll get an error. Remember, load event is removed, load method is not. “Load” method is still available, and it loads data from the server and places the returned HTML into the matched element. So how do we fix it?

jQuery team mentioned in their 3.0 release that .load.unload, and .error, deprecated since jQuery 1.8, are no more. Use .on() to register listeners. Therefore, use the following syntax to attach load event with windows object in jQuery 3.0

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

This should attach load event to windows object and it should work in all situations. But if the above code is not making a call to load event, then you can call the load event manually.

$(window).on('load', function() { ... }); //Attaches load event 
$(window).trigger('load'); //Triggers load event manually.

jQuery provides trigger event which execute all handlers and behaviors attached to the matched elements for the given event type.



Responsive Menu
Add more content here...