Question : JavaScript: Get first jpg image

I want to find the first jpeg or jpg image on a webpage.
 
1:
var jpg=document.getElementsByTagName('img')[0];


 That code finds the fist image but I want the first JPEG image, so I don't want it if it is a gif or png image.

Answer : JavaScript: Get first jpg image

http://www.w3schools.com/jsref/dom_obj_image.asp

Mime type is not readily available, so this may be wrong.  It assumes the file is loaded by name (a JPEG image can be retrieved via query, such as "/get_image?id=x")

var imgs = document.getElementsByTagName('img');
var jpg = null;
for(var i=0;i<imgs.length;i++) {
 if((imgs[i].src.substr(-5).toLower() == '.jpeg') || (imgs[i].src.substr(-4).toLower() == '.jpg')) {
   jpg=imgs[i];
   break;
 }
}
Random Solutions  
 
programming4us programming4us