toolman Posted October 30, 2014 Share Posted October 30, 2014 Hi, I am trying to redirect a page to another page based on if the page contains an image with an exact source. I have this so far: if ( $("span#bannerhold:has(img[src='http://www.website.com/images/banners/banner1.gif'])")){ location.href = "http://www.google.com" } However it doesn't seem to work. Any ideas why? Thanks! Link to comment https://forums.phpfreaks.com/topic/292165-if-page-contains-an-exact-image-src-then-redirect-to-another-page/ Share on other sites More sharing options...
codefossa Posted October 30, 2014 Share Posted October 30, 2014 Something like this would do it, but note that JS Fiddle won't allow you to get sent to another page. // http://jsfiddle.net/4d2xx7n1/ $(document).ready(function() { var url = "http://google.com", badImage = "http://darealtalk.files.wordpress.com/2013/02/smile.jpg"; $("img").each(function() { if ($(this).attr("src") != undefined && $(this).attr("src") == badImage) { if (confirm("Image Found! Click OK to redirect.")) $(location).attr("href", url); break; } }); }); Link to comment https://forums.phpfreaks.com/topic/292165-if-page-contains-an-exact-image-src-then-redirect-to-another-page/#findComment-1495279 Share on other sites More sharing options...
Alex_ Posted November 1, 2014 Share Posted November 1, 2014 That seems overly complicated Xaotique, considering he has an ID on the <span> tag it's kind of a waste to loop over all images on the page. Try something like this: //HTML <span id="bannerhold"><img src="http://www.website.com/images/banners/banner1.gif"></span> //JS $(document).ready(function() { var pageContainsImage = $('span#bannerhold > img[src="http://www.website.com/images/banners/banner1.gif"]').length > 0; if(pageContainsImage) { window.location.href = 'http://google.com'; } }); Link to comment https://forums.phpfreaks.com/topic/292165-if-page-contains-an-exact-image-src-then-redirect-to-another-page/#findComment-1495458 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.