Skip to content

Instantly share code, notes, and snippets.

@beratdogan
Created June 24, 2015 22:58
Show Gist options
  • Save beratdogan/615c7f2a7515550766e3 to your computer and use it in GitHub Desktop.
Save beratdogan/615c7f2a7515550766e3 to your computer and use it in GitHub Desktop.
Detecting if an image is really an image
request = new XMLHttpRequest()
request.onreadystatechange = ->
if request.readyState is 4
mimetype = request.getResponseHeader('content-type')
if mimetype.split('/').shift() is 'image'
alert 'Image!'
else
alert 'Not Image!'
request.open 'HEAD', 'http://upload.wikimedia.org/wikipedia/commons/5/5b/Ultraviolet_image_of_the_Cygnus_Loop_Nebula_crop.jpg', true
request.send()
request = new XMLHttpRequest()
request.open 'HEAD', 'http://upload.wikimedia.org/wikipedia/commons/5/5b/Ultraviolet_image_of_the_Cygnus_Loop_Nebula_crop.jpg', false
request.send()
mimetype = request.getResponseHeader('content-type')
if mimetype.split('/').shift() is 'image'
alert 'Image!'
else
alert 'Not Image!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment