Skip to content

Instantly share code, notes, and snippets.

@anderson-mota
Forked from cmichi/jQuery-file-exists.js
Last active December 14, 2015 01:29
Show Gist options
  • Save anderson-mota/5006588 to your computer and use it in GitHub Desktop.
Save anderson-mota/5006588 to your computer and use it in GitHub Desktop.
/**
* 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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment