Jump to content

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


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;
        }
    });
});
Edited by Xaotique

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';
}
 
});
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.