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’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 ...

article image
»

Using jQuery’s .resize() Method

jQuery’s .resize() method is a cool way to have an event ...

article image
»

How to Use jQuery to Search and Replace HTML

Have you ever made a mistake in your code or in your text and dreaded ...

article image
»

How to Use jQuery’s .mouseover() Method

jQuery’s .mouseover() method is used to execute some jQuery ...

article image
»

How to Use jQuery’s .blur() Method

jQuery’s .blur() method isn’t exactly what it sounds like ...

article image
»

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

article image
»

Set/Get/Delete Cookies with jQuery

There are a lot of plugins you can use to set, get, and delete ...

Screen Shot 2016-11-26 at 12.05.09 PM
»

3 Best jQuery Video Gallery Plugins

Do you have a lot of videos that you’d like to embed or neatly ...