Question : JavaScript: Get first jpg image - IE Problem

This code does not work with IE7
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

</head>
<body>

<p><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Face-smile.svg/48px-Face-smile.svg.png" alt="Image"></p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/1/1d/HP1_template.gif" alt="Image"></p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/8/88/Astronaut-EVA.jpg/48px-Astronaut-EVA.jpg" alt="Image"></p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Check.svg/48px-Check.svg.png" alt="Image"></p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/1/1a/Schackbr%C3%A4de_MVS.jpg" alt="Image"></p>

<script type="text/javascript">
/*<![CDATA[*/


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

if(jpg)  alert(jpg);
else alert('No JPEG was found.');

/*]]>*/
</script>

</body>
</html>
Related Solutions: JavaScript: Get first jpg image

Answer : JavaScript: Get first jpg image - IE Problem

Check this :

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Face-smile.svg/48px-Face-smile.svg.png" alt="Image"></p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/1/1d/HP1_template.gif" alt="Image"></p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/8/88/Astronaut-EVA.jpg/48px-Astronaut-EVA.jpg" alt="Image"></p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Check.svg/48px-Check.svg.png" alt="Image"></p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/1/1a/Schackbr%C3%A4de_MVS.jpg" alt="Image"></p>
<script>
		var pics = document.getElementsByTagName('img');
		var jpg = null;
		for(var i=0;i<pics.length;i++) {
		    jpg = pics[i].src;
		    if( /(\.jpg|\.jpeg)$/.test( pics[i].getAttribute("src") ) ) break;
		}
		if(jpg)  alert(jpg);
		else alert('No JPEG was found.');
</script>
</body>
</html>
Random Solutions  
 
programming4us programming4us