Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title>Responsive Images using Dynamic Base URL</title>
    
    <base href="http://placehold.it/1024/FF0000/">
    <script>
    (function(){
      var assetPath = 'http://placehold.it/';
      
      if( screen.width < 480 ){
          assetPath += '240/';
      }
      else if( screen.width < 960 ){
          assetPath += '480/';
      }
      else if( screen.width < 1024 ){
          assetPath += '960/';
      }
      else {
          assetPath += '1024/';
      }
      assetPath += '00FF00/';
      document.getElementsByTagName('base')[0].href = assetPath;
    })();
    </script>
    <style>
      .pass { color:green; }
      .pass:after { content:" (pass)"; }
      .fail { color:red; }
      .fail:after { content:" (fail)"; }
    </style>
  </head>
  <body>
    <h1>Responsive Images using Dynamic Base URL</h1>
    <p>Proof of concept for <a href="http://blog.keithclark.co.uk/responsive-images-using-cookies/#comment-16">Schepp's comment</a>  on Keith Clark's <a href="http://blog.keithclark.co.uk/responsive-images-using-cookies/">Responsive images using cookies</a>. No cookies needed!!!</p>
    <p>Image src: <span id=src>Loading…</span></p>
    <img src="FFF.png&text=ResponsiveImage">
  </body>
</html>
 
window.onload  = function(e){
    var srcEl = document.getElementById('src');
    srcEl.textContent = document.images[0].src;
    srcEl.className += src.textContent.indexOf('00FF00') != -1 ? 'pass' : 'fail';
};
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers