Skip to content

Instantly share code, notes, and snippets.

@cmichi
Created November 30, 2010 22:56
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cmichi/722584 to your computer and use it in GitHub Desktop.
Save cmichi/722584 to your computer and use it in GitHub Desktop.
Very simple way to check if a file exists, using jQuery.
/**
* Very simple way to check if a file exists on this domain.
* Use with the jQuery library.
*
* Important: Works only on the same domain.
* Cross-domain-requests have to be done in another way (see JSONP)!
*
* Use: console.log( "/data/list.json".fileExists() );
*/
String.prototype.fileExists = function() {
filename = this.trim();
var response = jQuery.ajax({
url: filename,
type: 'HEAD',
async: false
}).status;
return (response != "200") ? false : true;
}
@lpguo
Copy link

lpguo commented Jun 28, 2013

This way will throw an error "Failed to load resource" in google chrome dev version. Can you provide an other better way? I encounter the problem. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment