How to check for retina display in jQuery and serve double-resolution images accordingly?
<p>I have a little JS-script that checks window-width on resize event and serves different full-screen background images specified on the html-side in data-attributes accordingly. Unfortunately, I cannot use a CSS-solution in this particular case.</p>

<p>The solution on its own works, but it does not incorporate images for retina displays. If I see this correctly, images for retina displays need twice the resolution to be displayed correctly. I have set the following breakpoints in the script: 1280 x 720px (retina: 2560 x 1440) , 1920 x 1080 px (retina: 3840 x 2160), larger than 2560 x 1440 px (retina: 5120 x 2880).</p>

<p><strong>HTML:</strong></p>

<pre><code>&lt;section id="page-title" class="page-title-lg img-cover header-lg" data-bg-xxl="test/2560.jpg" data-bg-xl="test/fullhd.jpg" data-bg-lg="test/hdready.jpg"&gt;
    &lt;div class="container"&gt;
      &lt;div class="page-title-wrapper"&gt;
        &lt;div class="page-title-txt"&gt;
          &lt;h1&gt;Large Page Title&lt;/h1&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/section&gt;
</code></pre>

<p><strong>JS:</strong></p>

<pre><code>function checkWidth(){

    var screenWidth = $window.width();

    if( screenWidth &lt;= 1280 ){
        // Hd-ready desktop - 1280 x 720
        $('[data-bg-lg]').each(function() {
            var dataImg = $(this).attr('data-bg-lg');
            $(this).css('background-image', 'url(assets/img/' + dataImg + ')');
        });
    } else if ( screenWidth &gt;= 1281 &amp;&amp; screenWidth &lt;= 1920 ) {
        // Full-hd desktop - 1920 x 1080
        $('[data-bg-xl]').each(function() {
            var dataImg = $(this).attr('data-bg-xl');
            $(this).css('background-image', 'url(assets/img/' + dataImg + ')');
        });
    } else {
        // Huge desktop - 2560 x 1440
        $('[data-bg-xxl]').each(function() {
            var dataImg = $(this).attr('data-bg-xxl');
            $(this).css('background-image', 'url(assets/img/' + dataImg + ')');
        });
    }

}
</code></pre>

<p>The question now is how to check for a retina / non-retina display and serve the correct image set with the double resolution accordingly? </p>