0

I'm currently trying to capture the image URL within this Javascript call below, I'm struggling to get my head around the code.

<a href="javascript:fnChangeImage('.ItemImage.Main','http://www.forever21.com/images/5_detail_750/00185472-01.jpg');">

I have the following code fetching the title, price etc perfectly but I'm not too sure how to grab something from a javascript call.

var result = {
    productName: $(".pdp_title .item_name_p").text().trim(),
    description: $(".d_content span").text().trim(),
    price: $(".pdp_title .price_p").text(),
    imageUrls: []
};
2
  • What does fnChangeImage do? Commented May 4, 2016 at 8:59
  • 1
    @mplungjan I think he doesn't care, it seems like he is trying to scrape some data and he is interested in the image url passed to the function Commented May 4, 2016 at 9:09

1 Answer 1

3

Here is a one liner

var imageUrl = /.*,'(.+)'/.exec($("a[href^='javascript:fnChangeImage']").attr("href"))[1];

How it works

  1. Select the dom element that is an anchor link and it's href starts with javascript:fnChangeImage
  2. Apply regex pattern /.*,'(.+)'/ on the selected element href attribute
  3. The image url is captured in the first matching regex group.
Sign up to request clarification or add additional context in comments.

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.