0

hey so i'm trying to use onerror with img tag which will check if website is up or not. According to my research i have done onerror can be used to get status for website. So what i'm trying to accomplish is if website has an 404 it will return an image which red color however if website is up and running then it will show an green image. So i was able to come up with a code that does the trick but issue is i want to combine the code so it will only show one image at a time except two below is sample of my code any help will be apprecaited.

<img src="badlink" onerror="this.onerror=null;this.src='https://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Red.svg/120px-Red.svg.png'" />

<img src="https://www.google.com" onerror="this.onerror=null;this.src='http://greensportsalliance.org/images/darkGreenSquare.gif'" />
helpmeplz
  • 11
  • 2
  • in both img tag you are using this,onerror=null – user786 Jun 21 '15 at 18:41
  • Im trying to check if google is up or not on both of the images if its up it will show green image but if google is down then it will show an red image. – helpmeplz Jun 21 '15 at 18:49
  • possible duplicate of [Detect links to non-existent websites using JavaScript](http://stackoverflow.com/questions/14422560/detect-links-to-non-existent-websites-using-javascript) – vinayakj Jun 21 '15 at 19:59

1 Answers1

1
<img src="badlink" onerror="onerror=null;this.src='https://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Red.svg/120px-Red.svg.png'" />

<img src="https://www.google.com" onerror="onerror=null;this.src='http://greensportsalliance.org/images/darkGreenSquare.gif'" />
vinayakj
  • 5,591
  • 3
  • 28
  • 48
  • Sorry if i was bad at explaining so im trying to use img to check status of website. If there is 404 on a website then the image will be red but if the website is up and running then the image will be green. – helpmeplz Jun 21 '15 at 18:52
  • img tag searches for image so even if the link given is up but not image source then it would call onerror – vinayakj Jun 21 '15 at 19:21
  • Is there any other solution i have html page – helpmeplz Jun 21 '15 at 19:41
  • Here is the exact answer http://stackoverflow.com/questions/14422560/detect-links-to-non-existent-websites-using-javascript?lq=1#answer-14422613 – vinayakj Jun 21 '15 at 19:58
  • Can this method be used somehow?http://jsfiddle.net/0s5fb0nb/ my server is not based on php the issue – helpmeplz Jun 21 '15 at 20:29