X

LATEST #JQUERY NEWS

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 load event 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.

Recently Published

article image
»

jQuery Snippets: How to Add/Remove Classes After Delay

In your projects, there can often be instances of wanting to change ...

article image
»

jQuery Snippets: Back to Top Button

One common way to use jQuery is to add a useful and functional ...

article image
»

jQuery .on() Method: the Issue of Dynamically Added Elements

The jQuery .on() method is the recommended way for attaching events ...

article image
»

How to Use jQuery to Test CSS3 Browser Support

The tricky thing about using CSS properties (especially newer ones ...

article image
»

jQuery Snippets: Check/Un-Check All Function

This easy-to-implement jQuery code snippet is super useful, and can ...

article image
»

Useful jQuery Code Snippets for Handling CSS

If you’re not already familiar with code snippets, they are ...

article image
»

Using Method Chaining in jQuery

Using method chaining in jQuery helps to ensure that your code stays ...

article image
»

jQuery’s .children() Method

Just like in CSS, it’s possible to use jQuery to select the ...

article image
»

jQuery’s .click() Method

With jQuery, it’s super easy to make something happen as a result ...