Created
November 30, 2010 22:56
-
-
Save cmichi/722584 to your computer and use it in GitHub Desktop.
Very simple way to check if a file exists, using jQuery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.