Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript With JavaScript: Detect an image src and replace it…

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #32810
    barkins
    Member

    Hi there,

    I’m looking for some JavaScript to detect whether a photo on the page exists based on it’s URL and then replace that URL.

    Something like,

    If img.src ==http://google.com THEN img.src=http://yahoo.com

    Thank you for any help in advance.

    #75596
    IsaacAndela
    Member

    With jQuery it is fairly easy.

    The small script below loops through each image element, checks if the src matches and if so replaces it.

    // Loop trough each image element and replace the src if the src matches
    $("img").each(function(i, elem){
    // If src of img element matches a certain URL then src is new url
    if ($(this).attr("src") == "http://google.com"){
    $(this).attr("src", "http://yahoo.com");
    }
    });
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.