How to replace an HTML image with an alternative image?












2















I am building a web application, it is catalogus for static ship information, I collect data from different sources. One of them is https://www.marinetraffic.com/, I use this source also to retireve an image of the ship. Retrieving an image is as simple as this:
<img src="https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi=273914200>



That will load this image:



Note mmsi part in the url this is one possible identifier a ship can have, the problem is that ships can have multiple identifiers so is there also a imo number, callSign and eni number. On https://photos.marinetraffic.com/ it is possible to search for ships on both imo or mmsi number to retrieve an image.



So this <img src="https://photos.marinetraffic.com/ais/showphoto.aspx?imo=6600553">



Returns the same image:



For this particular ship I have both identifiers in my database, but there are also ships with only one identifier. For example I have this ship with only imo number 7361518 when I want to retrieve the image I try to do this:



<img src="https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518"
But that returns a 404 error.



Currently I solved it doing this:



<img width="100%" height="100%" src={`https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi={id}`}/>
<img width="100%" height="100%" src=`https://photos.marinetraffic.com/ais/showphoto.aspx?imo={id}`}/>


So I am placing two image tags above each other passing the ship id value to the url, the id can be either imo or mmsi number, this causes on of the images not to load.



this causes in a result like this:
enter image description here



Note the not loaded icon at hte bottom, I am thniking of some sollutuion but cant figure out how to do it:




  1. Check the HTTP status code and then display the image

  2. Display an alternative image when ship does not load with mms with a fallback to imo

  3. Remove the image not loaded icon, to make the image invisible when image has no result.


So after this whole explanation of a case study, a real wrold scenario and the image problem does anyone know an implementation to any of my sollutions or have an addiotional sollution for this problem?










share|improve this question























  • I just tested your id with both imo and mmsi links. Imo works, are you sure you're trying to fetch the correct image ? Example used : photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518

    – Mojo Allmighty
    Nov 14 '18 at 14:32













  • Yes both work, photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518 does not work offcourse. When you try to search by mmsi and pass a imo number. My question is more about how to us photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518 as a fallback for photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518

    – Harry Stylesheet
    Nov 14 '18 at 14:43













  • Best solution is to use javascript. here is a nice answer for your problem : stackoverflow.com/a/38359177/6949388

    – Mojo Allmighty
    Nov 14 '18 at 15:08











  • I am using React too, so I will look into it

    – Harry Stylesheet
    Nov 14 '18 at 15:36
















2















I am building a web application, it is catalogus for static ship information, I collect data from different sources. One of them is https://www.marinetraffic.com/, I use this source also to retireve an image of the ship. Retrieving an image is as simple as this:
<img src="https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi=273914200>



That will load this image:



Note mmsi part in the url this is one possible identifier a ship can have, the problem is that ships can have multiple identifiers so is there also a imo number, callSign and eni number. On https://photos.marinetraffic.com/ it is possible to search for ships on both imo or mmsi number to retrieve an image.



So this <img src="https://photos.marinetraffic.com/ais/showphoto.aspx?imo=6600553">



Returns the same image:



For this particular ship I have both identifiers in my database, but there are also ships with only one identifier. For example I have this ship with only imo number 7361518 when I want to retrieve the image I try to do this:



<img src="https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518"
But that returns a 404 error.



Currently I solved it doing this:



<img width="100%" height="100%" src={`https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi={id}`}/>
<img width="100%" height="100%" src=`https://photos.marinetraffic.com/ais/showphoto.aspx?imo={id}`}/>


So I am placing two image tags above each other passing the ship id value to the url, the id can be either imo or mmsi number, this causes on of the images not to load.



this causes in a result like this:
enter image description here



Note the not loaded icon at hte bottom, I am thniking of some sollutuion but cant figure out how to do it:




  1. Check the HTTP status code and then display the image

  2. Display an alternative image when ship does not load with mms with a fallback to imo

  3. Remove the image not loaded icon, to make the image invisible when image has no result.


So after this whole explanation of a case study, a real wrold scenario and the image problem does anyone know an implementation to any of my sollutions or have an addiotional sollution for this problem?










share|improve this question























  • I just tested your id with both imo and mmsi links. Imo works, are you sure you're trying to fetch the correct image ? Example used : photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518

    – Mojo Allmighty
    Nov 14 '18 at 14:32













  • Yes both work, photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518 does not work offcourse. When you try to search by mmsi and pass a imo number. My question is more about how to us photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518 as a fallback for photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518

    – Harry Stylesheet
    Nov 14 '18 at 14:43













  • Best solution is to use javascript. here is a nice answer for your problem : stackoverflow.com/a/38359177/6949388

    – Mojo Allmighty
    Nov 14 '18 at 15:08











  • I am using React too, so I will look into it

    – Harry Stylesheet
    Nov 14 '18 at 15:36














2












2








2








I am building a web application, it is catalogus for static ship information, I collect data from different sources. One of them is https://www.marinetraffic.com/, I use this source also to retireve an image of the ship. Retrieving an image is as simple as this:
<img src="https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi=273914200>



That will load this image:



Note mmsi part in the url this is one possible identifier a ship can have, the problem is that ships can have multiple identifiers so is there also a imo number, callSign and eni number. On https://photos.marinetraffic.com/ it is possible to search for ships on both imo or mmsi number to retrieve an image.



So this <img src="https://photos.marinetraffic.com/ais/showphoto.aspx?imo=6600553">



Returns the same image:



For this particular ship I have both identifiers in my database, but there are also ships with only one identifier. For example I have this ship with only imo number 7361518 when I want to retrieve the image I try to do this:



<img src="https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518"
But that returns a 404 error.



Currently I solved it doing this:



<img width="100%" height="100%" src={`https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi={id}`}/>
<img width="100%" height="100%" src=`https://photos.marinetraffic.com/ais/showphoto.aspx?imo={id}`}/>


So I am placing two image tags above each other passing the ship id value to the url, the id can be either imo or mmsi number, this causes on of the images not to load.



this causes in a result like this:
enter image description here



Note the not loaded icon at hte bottom, I am thniking of some sollutuion but cant figure out how to do it:




  1. Check the HTTP status code and then display the image

  2. Display an alternative image when ship does not load with mms with a fallback to imo

  3. Remove the image not loaded icon, to make the image invisible when image has no result.


So after this whole explanation of a case study, a real wrold scenario and the image problem does anyone know an implementation to any of my sollutions or have an addiotional sollution for this problem?










share|improve this question














I am building a web application, it is catalogus for static ship information, I collect data from different sources. One of them is https://www.marinetraffic.com/, I use this source also to retireve an image of the ship. Retrieving an image is as simple as this:
<img src="https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi=273914200>



That will load this image:



Note mmsi part in the url this is one possible identifier a ship can have, the problem is that ships can have multiple identifiers so is there also a imo number, callSign and eni number. On https://photos.marinetraffic.com/ it is possible to search for ships on both imo or mmsi number to retrieve an image.



So this <img src="https://photos.marinetraffic.com/ais/showphoto.aspx?imo=6600553">



Returns the same image:



For this particular ship I have both identifiers in my database, but there are also ships with only one identifier. For example I have this ship with only imo number 7361518 when I want to retrieve the image I try to do this:



<img src="https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518"
But that returns a 404 error.



Currently I solved it doing this:



<img width="100%" height="100%" src={`https://photos.marinetraffic.com/ais/showphoto.aspx?mmsi={id}`}/>
<img width="100%" height="100%" src=`https://photos.marinetraffic.com/ais/showphoto.aspx?imo={id}`}/>


So I am placing two image tags above each other passing the ship id value to the url, the id can be either imo or mmsi number, this causes on of the images not to load.



this causes in a result like this:
enter image description here



Note the not loaded icon at hte bottom, I am thniking of some sollutuion but cant figure out how to do it:




  1. Check the HTTP status code and then display the image

  2. Display an alternative image when ship does not load with mms with a fallback to imo

  3. Remove the image not loaded icon, to make the image invisible when image has no result.


So after this whole explanation of a case study, a real wrold scenario and the image problem does anyone know an implementation to any of my sollutions or have an addiotional sollution for this problem?







html image






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:27









Harry StylesheetHarry Stylesheet

1




1













  • I just tested your id with both imo and mmsi links. Imo works, are you sure you're trying to fetch the correct image ? Example used : photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518

    – Mojo Allmighty
    Nov 14 '18 at 14:32













  • Yes both work, photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518 does not work offcourse. When you try to search by mmsi and pass a imo number. My question is more about how to us photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518 as a fallback for photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518

    – Harry Stylesheet
    Nov 14 '18 at 14:43













  • Best solution is to use javascript. here is a nice answer for your problem : stackoverflow.com/a/38359177/6949388

    – Mojo Allmighty
    Nov 14 '18 at 15:08











  • I am using React too, so I will look into it

    – Harry Stylesheet
    Nov 14 '18 at 15:36



















  • I just tested your id with both imo and mmsi links. Imo works, are you sure you're trying to fetch the correct image ? Example used : photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518

    – Mojo Allmighty
    Nov 14 '18 at 14:32













  • Yes both work, photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518 does not work offcourse. When you try to search by mmsi and pass a imo number. My question is more about how to us photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518 as a fallback for photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518

    – Harry Stylesheet
    Nov 14 '18 at 14:43













  • Best solution is to use javascript. here is a nice answer for your problem : stackoverflow.com/a/38359177/6949388

    – Mojo Allmighty
    Nov 14 '18 at 15:08











  • I am using React too, so I will look into it

    – Harry Stylesheet
    Nov 14 '18 at 15:36

















I just tested your id with both imo and mmsi links. Imo works, are you sure you're trying to fetch the correct image ? Example used : photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518

– Mojo Allmighty
Nov 14 '18 at 14:32







I just tested your id with both imo and mmsi links. Imo works, are you sure you're trying to fetch the correct image ? Example used : photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518

– Mojo Allmighty
Nov 14 '18 at 14:32















Yes both work, photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518 does not work offcourse. When you try to search by mmsi and pass a imo number. My question is more about how to us photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518 as a fallback for photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518

– Harry Stylesheet
Nov 14 '18 at 14:43







Yes both work, photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518 does not work offcourse. When you try to search by mmsi and pass a imo number. My question is more about how to us photos.marinetraffic.com/ais/showphoto.aspx?imo=7361518 as a fallback for photos.marinetraffic.com/ais/showphoto.aspx?mmsi=7361518

– Harry Stylesheet
Nov 14 '18 at 14:43















Best solution is to use javascript. here is a nice answer for your problem : stackoverflow.com/a/38359177/6949388

– Mojo Allmighty
Nov 14 '18 at 15:08





Best solution is to use javascript. here is a nice answer for your problem : stackoverflow.com/a/38359177/6949388

– Mojo Allmighty
Nov 14 '18 at 15:08













I am using React too, so I will look into it

– Harry Stylesheet
Nov 14 '18 at 15:36





I am using React too, so I will look into it

– Harry Stylesheet
Nov 14 '18 at 15:36












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302502%2fhow-to-replace-an-html-image-with-an-alternative-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302502%2fhow-to-replace-an-html-image-with-an-alternative-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine