views:

34

answers:

1

Hi all,

I don't know if this is even possible in Javascript as am pretty new to it, but hopefully someone will be able to help!

I want some Javascript (ideally jQuery) that will follow all the links with a certain word in on a page and then on the next page, there will be a link to an image. I want to copy that link and then go back and change the original link to the image link.

Does that make any sense? Am happy to clarify if not!

Thank you

A: 

Here: http://vidasp.net/tinydemos/fetch-image-links.html

This is the code that does the trick:

$("a:contains('image')").click(function() {
    var url = $(this).attr("href");
    $.get(url, function(data) {
        location.href = $(data).find("img").attr("src");
    });
    return false;
});
Šime Vidas
In your example on your website, it doesn't seem to link directly to the image? It still links to http://vidasp.net/tinydemos/image1.html rather than http://vidasp.net/media/illustration.jpg?
James Sampson
Ah, seems to work now for some reason?!
James Sampson
What browser are you using? I just tested in Chrome, Firefox, Opera, Safari and IE9 beta and it works. I don't have older versions of IE here ...
Šime Vidas
I guess, jQuery failed to load the first time... or something like that.
Šime Vidas
Did you include the above jQuery code to your site?
Šime Vidas
Hi, yeah I did. I realised I hadn't changed the 'image' bit to the word I want. However, it now picks up the wrong image! Is there a way to get it to only pick up images with a certain word in their url? Also, it doesn't seem to actually be changing the url to end in .jpg? Only redirecting when I click on it. I really need it to change the actual url? Thank you so much!
James Sampson
Having looked at the source, all I actually need to do for that link is copy the title of the link to the href of the link and it should work, so going to give that a go!
James Sampson