jQuery: .contains() vs .indexOf()

Posted on Nov 07, 2013 by Nate in Uncategorized

I recently worked on a project that needed to dynamically load a video, based on a url supplied by the user. To parse the url, I used jQuery’s .contains() method. Initially, everything worked great. But once I began testing the mobile version of the site, I noticed that my video wasn’t loading correctly.

It took a while to track the problem down, because I was sure the issue spawned from some of the custom code I had written to load the video. Instead, I finally found the problem when, on a whim, I switched out .contains() with .indexOf(). For some reason, the mobile browser failed at .contains(), and my code was never returning true.

So if you run into an unexpected error on your site that only occurs on mobile devices, check your code for the .contains() method. It could be causing your problem.

Leave a Reply

Your email address will not be published. Required fields are marked *