tags:

views:

52

answers:

2
+3  Q: 

jQuery if exist

How to check, is file exist on current site?

Like:

if ('http://site.com/girlfirend.png' exist) {
// do something
}

Maybe I can use .get? But how to check its status (ok or 404)?

Thanks.

+3  A: 

Issue a get request to the file and inspect the result.

See Ajax in jquery

rahul
You can do such a request with ajax, and specify you only want the HEAD (in order to check the response code).
greg0ire
How to? I don't understand
Happy
Check this one. http://stackoverflow.com/questions/197228/how-to-check-if-a-file-exists-in-javascript
rahul
@greg0ire, quoth jQuery docs: "The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers."
Piskvor
+1  A: 

Use jQuery, this has been asked here:

http://stackoverflow.com/questions/197228/how-to-check-if-a-file-exists-in-javascript

Kerry
I have to check without $("#someDivId")
Happy
Then @rahul looks like he has a good answer
Kerry