Jump to content

if page contains an exact image src then redirect to another page


toolman

Recommended Posts

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!

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;
        }
    });
});

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';
}
 
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.