javascript - Read response headers from image error handler -


i have <img /> element in page, , it's src attribute regularly changed javascript.

when src attribute changes, code on servers checks :

  1. is user logged in ?
  2. does image exist ?

then sends response accordingly, either image or response error headers (status 403 forbidden or 404 not found).

here's problem : handle img error following code

$(function(){     var $img = $('#test_image');     $img.on('error',function(e){         // here, need read headers (at least status code)         console.log(e);     }).attr('src','http://example.com/loadimg.php?img=3'); }); 

i need display distinct error message if user can't load image because session has expired or if image he's trying load doesn't exist.

where can find response headers and/or status code in eventobject handler ?

thank you.

jsfiddle

if overhead isn't you, send head request see return in case of error

var http = new xmlhttprequest(); http.open('head', url, true); // true async http.send(); if (http.status == 404) {     // } else if (http.status == 403) {     // } else {     // } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -