2

I looking for way to show the domain name of images from images tag

<p>aabb .</p><br><center>
<img itemprop="image" src="http://subww.somedomain.com.uk/images/news/Image/2013/09/20/6523c3850ef881.img.jpg" alt="bbddd ddd" class="dddd" border="0" height="225" width="300"><br><i>lkjlkkjf</i></center>
<img src="http://www.somebbbdomain.com/images/news/Image/2013/09/20/6523c3850ef881.img.jpg" alt="bbddd ddd">
<div style="clear: both;"></div><p>aabbcc</p>

The javascript will look for fist src of images then extract alert domain name.. "somedomain.com.uk" to screen.

Binh Nguyen
  • 1,313
  • 3
  • 17
  • 27

1 Answers1

1

do like:

var imgEles = document.getElementsByTagName("img"),
    pattern = /^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i,
    domains = [];
for( var im = 0; im < imgEles.length; im++) {
   var src = imgEles[im].src;
   var matches = src.match(pattern);
   var domain = matches && matches[1]; 
   domains.push( domain );
}
console.log( domains );
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • thanhk very much .. this is great short one compair to other http://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string .. thanks again – Binh Nguyen Sep 23 '13 at 06:35