title: HTML/JavaScript - Remote img src with automatic http/https prefix
body: <p>Displaying an image from a remote server:</p>
  
  <pre><code>&lt;img src="http://remotehost/path/to/a.png" alt="Image A" /&gt;
  </code></pre>
  
  <p>However, if the current page is accessed via HTTPS, linking images via unencrypted HTTP will yield security warnings. While I could just specify https:// regardless of the current protocol, doing so would be wasteful since I really don't care to secure the transmission of this image unless it is necessary (when the visitor is using HTTPS).</p>
  
  <p>Is it possible to specify a URL for the src attribute of the img tag such that the protocol in the URL is dynamically chosen based on the protocol used to access the current page? To illustrate what I mean:</p>
  
  <pre><code>&lt;img src="(JavaScript's window.location.protocol)//remotehost/path/to/a.png" alt="Image A" /&gt;
  </code></pre>
  
  <p>What if we use JavaScript? We could give the img tag an id so that we can locate it and set the src to start with window.location.protocol.</p>
  
  <p>Could also use base64 to bypass the HTTP/HTTPS problem altogether, but this isn't ideal for what I'm doing.</p>
  
  <p>What can you guys recommend?</p>