最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I check if the href path contain image or someother link using javascript or jquery? - Stack Overflow

programmeradmin4浏览0评论

Any One Know Tell me the suggestion to do this. How can i check if the anchor href attribute contain image path or some other path.

For Example:

    <a href="image.jpg"><img src="image.jpg"/></a>
    <a href=""><img src="image.jpg"/></a>

See the above example shows href attribute contain different path like first one is the image and second one is the some other site link. I still confuse with that how can i check if the href path contain the image path or some other path using jquery or javascript.

Any suggestion would be great.

Any One Know Tell me the suggestion to do this. How can i check if the anchor href attribute contain image path or some other path.

For Example:

    <a href="image.jpg"><img src="image.jpg"/></a>
    <a href="http://google."><img src="image.jpg"/></a>

See the above example shows href attribute contain different path like first one is the image and second one is the some other site link. I still confuse with that how can i check if the href path contain the image path or some other path using jquery or javascript.

Any suggestion would be great.

Share Improve this question edited Jul 10, 2013 at 6:35 mishik 10k9 gold badges46 silver badges68 bronze badges asked Jul 10, 2013 at 6:29 Vignesh PichamaniVignesh Pichamani 8,07022 gold badges80 silver badges117 bronze badges 9
  • 1 How are you getting the anchor element that you want to test? – John Dvorak Commented Jul 10, 2013 at 6:32
  • ~[...].indexOf(a.href.slice(-3)) would be a reasonable approximation if a points to the anchor element. – John Dvorak Commented Jul 10, 2013 at 6:33
  • I want to check it dynamically for every image. If the href attribute contain image path then i going to do something like – Vignesh Pichamani Commented Jul 10, 2013 at 6:34
  • I mean, do you have a reference to the anchor element? A reference to its jQuery wrapper? Only an HTML string containing it? – John Dvorak Commented Jul 10, 2013 at 6:34
  • 2 Just looking to see if a file has .jpg, .png or .gif extension isn't really enough to prove that the file is or isn't an image, especially when checking links. – Brian Hoover Commented Jul 10, 2013 at 6:37
 |  Show 4 more ments

3 Answers 3

Reset to default 6

For example (you may need to include other pic formats if needed):

$("a").each(function(i, el) {
  var href_value = el.href;
  if (/\.(jpg|png|gif)$/.test(href_value)) {
     console.log(href_value + " is a pic");
  } else {
     console.log(href_value + " is not a pic");
  }
});

Jquery:

$(document).ready( function() {
  var checkhref = $('a').attr('href');
  var image_check = checkhref.substr(checkhref.length - 4)
  http_tag = "http";
  image = [".png",".jpg",".bmp"]
  if(checkhref.search("http_tag") >= 0){
    alert('Http!');
    //Do something
  }
  if($.inArray(image_check, image) > -1){
    alert('Image!');
    //Do something
  }
});

you may check if image exists or not, without jQuery

Fiddle

 var imagesrc = 'http://domain./image.jpg';
function checkImage(src) {
var img = new Image();
img.onload = function() {
    document.getElementById("iddiv").innerHTML = src +" exists";

};
img.onerror = function() {
    document.getElementById("iddiv").innerHTML = src +"does not exists";
};
img.src = src; // fires off loading of image
return src;
}
checkImage(imagesrc);
发布评论

评论列表(0)

  1. 暂无评论