0

I'm scraping some content and there's this image which has this:

<div id="targetImage"><img src="alfresco/nap/webAssets/webPage/homepage/desktop/common/intl/global/images/primary-v=1.13.jpg" alt="" title="" width="550" height="682"></div>

I need to append

"http://www.targetdomain.com/"

before

"alfresco" 

What is the best method to achieve this via jquery? Do I need regex for this?

Thanks

2
  • Is that HTML on your page or is it in a string obtained by your scraper? How are you scraping in the first place? Commented Mar 23, 2013 at 13:02
  • remember that you can always use <BASE> Commented Mar 23, 2013 at 13:08

3 Answers 3

2
var img= $("#targetImage img:first");
img.attr("src", "http://www.targetdomain.com/"+img.attr("src"));

Here is a fiddle for you http://jsfiddle.net/9ajgj/1/

Sign up to request clarification or add additional context in comments.

Comments

2

The following should work:

var img = $("#targetImage").children("img")[0];
img.attr("src", "http://www.targetdomain.com/"+img.attr("src"));

Comments

0
var root = 'http://www.targetdomain.com/';
$('.someContent').find('img').each(function(index, image) {
    image.src.indexOf('http') !== 0 && (image.src = root + image.src);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.