Easily check for file existence using jQuery

Today I had some difficulty in checking the existence of a jsonp file. Turns out that jsonp doesn’t return a traditional ajax 404 error if the file doesn’t exist. So I wrote a nifty hack to get around it.

The important parts are to only do a HEAD lookup or you will grab the entire file, be sure to set a timeout (especially in the instance of jsonp, which as I mentioned, won’t return a traditional 404) and be sure to set your dataType correctly.

The code:

Or see it in action on jsFiddle.

Author avatar


    1. Hey Mohamed, glad it helped! It’s really just a timeout, but sometimes things like that are less obvious (at least it was for me at first). 🙂

Comments are closed.

We use cookies to give you the best experience. Cookie Policy