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
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...