- If the image exists, the onload events triggers (obviously)
- If the image does not exist on the remote server the onerror event triggers (after a very short time)
- If the server does not exists the onerror event triggers after a longer period of time.
This behaviour can be used to ‘ping’ a hosts on the local network of the user:
- Create image with src attribute pointing to a local IP address.
- Set a timeout for the image (~ 1000ms).
- The onerror event fires after a short period of time -> the local host exist (because the image is not present)
- The onerror event fires after a long period of time -> the timeout gets triggered before -> there is no local host with this IP address.
- The onload event fires -> The host is exists, and the image is present.
Note that only hosts running with a open port 80 can be detected this way. In combination with AJAX I made a short PoC, which provides astonishing good results. You can run the live demo here.