0

I am making an on-line shop for selling magazines, and I need to show the image of the magazine. For that, I would like to show the same image that is shown in the website of the company that distributes the magazines.

For that, it would be easy with an absolute path, like this:

<img src="http://www.remotewebsite.com/image.jpg" />

But, it is not possible in my case, because the name of the image changes everytime there is a new magazine.

In Javascript, it is possible to get the path of an image with this code:

var strImage = document.getElementById('Image').src;

But, is it possible to use something similar to get the path of an image if it is in another HTML page?

9
  • 4
    Have you asked the other web site if they have permanent links to the images? It would make it much easier for you if they do. Commented Sep 6, 2018 at 13:27
  • You are looking for the term webscraping. On the backend, something like that is quite easy to implement, but on the frontend it's rather "hacky" and inefficient. Commented Sep 6, 2018 at 13:28
  • @CodeF0x — … and usually impossible due to the SOP. Commented Sep 6, 2018 at 13:28
  • 1
    How likely is it that the images changes multiple times a day? webcrawl all the sites you want (1x or N-times a day) and store the image url in your database. Commented Sep 6, 2018 at 13:29
  • @Quentin Yep, that's why I chose the word "hacky". Commented Sep 6, 2018 at 13:29

3 Answers 3

1

Assuming that you know how to find the correct image in the magazine website's DOM (otherwise, forget it):

  • the magazine website must explicitly allow clients showing your website to fetch their content by enabling CORS
  • you fetch their HTML -> gets you a stream of text
  • parse it with DOMParser -> gets you a Document
  • using your knowledge or their layout (or good heuristics, if you're feeling lucky), use regular DOM navigation to find the image and get its src attribute

I'm not going to detail any of those steps (there are already lots of SO answers around), especially since you haven't described a specific issue you may have with the technical part.

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

Comments

0

You can, but it is inefficient. You would have to do a request to load all the HTML of that other page and then in that HTML find the image you are looking for.

It can be achieved (using XMLHttpRequest or fetch), but I would maybe try to find a more efficient way.

Comments

0

What you are asking for is technically possible, and other answers have already gone into the details about how you could accomplish this.

What I'd like to go over in this answer is how you probably should architect this given the requirements that you described. Keep in mind that what I am describing is one way to do this, there are certainly other correct methods as well.

Create a database on the server where your app will live. A simple MySQL DB will work, but you could use anything. Create a table called magazine, with a column url. Your code would pull the url from this DB. Whenever the magazine URL changes, just update the DB and the code itself won't need to be changed.

Your front-end code needs some sort of way to access the DB. One possible solution is a REST API. This code would query the DB for the latest values (in your case magazine URLs), and make them accessible to your web page. This could be done in a myriad of different languages/frameworks, here's a good tutorial on doing something like this in Node.js and express (which is what I'd personally use).

Finally, your front-end code needs to call your REST API to get the updated URLs. This needs to be done with some kind of JavaScript based language. jQuery would make this really easy, something like this:

$(document).ready(function() {
  $.Get("http://uri_to_your_rest_api", function(data) {
    $("#myImage").attr("scr", data.url);
  }
});

Assuming you had HTML like this:

<img id="myImage" src="">

And there you go - You have a webpage that pulls the image sources dynamically from your database.

Now if you're just dipping your toes into web development, this may seem a bit overwhelming. But I promise you, in the long run it'll be easier then trying to parse code from an HTML page :)

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.