Example

by tinyhustlee

HTML

<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/crossOrigin -->

<div class="container">
  <p>Here's a paragraph. It's a very interesting paragraph. You
  are captivated by this paragraph. Keep reading this paragraph.
  Okay, now you can stop reading this paragraph. Thanks for
  reading me.</p>
</div>

CSS

body {
  font: 1.125rem/1.5, Helvetica, sans-serif;
}

.container {
  display: flow-root;
  width: 37.5em;
  border: 1px solid #d2d2d2;
}

img {
  float: left;
  padding-right: 1.5em;
}

output {
  background: rgba(100, 100, 100, 0.1);
  font-family: Courier, monospace;
  width: 95%;
}

JavaScript

const imageUrl = "https://mdn.mozillademos.org/files/16797/clock-demo-400px.png";
const container = document.querySelector(".container");

function loadImage(url) {
  const image = new Image(200, 200);
  image.addEventListener("load",
    () => container.prepend(image)
  );

  image.addEventListener("error", () => {
    const errMsg = document.createElement("output");
    errMsg.value = `Error loading image at ${url}`;
    container.append(errMsg);
  });
 
  image.crossOrigin = "use-credentials";
  image.alt = "";
  image.src = url;
}

loadImage(imageUrl);