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! Quote Link to comment Share on other sites More sharing options...
codefossa Posted October 30, 2014 Share Posted October 30, 2014 (edited) 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; } }); }); Edited October 30, 2014 by Xaotique Quote Link to comment 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'; } }); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.