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 ifa
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
3 Answers
Reset to default 6For 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);