JavaScript replace all css background images with retinal images

If you are faced with the problem, that dynamically generated CSS background images used, to images on a Web page to represent and this then also for retina display you want to look good, can I use the following script. It additionally checked via an AJAX request, If the images exist / exist.

A prerequisite is, that a naming convention for non-retina used images and retina is such as:

for non-retinal images

and

for retinal images

<!--Retina Fix for dynamic background images-->
        <script type="text/javascript">
            function isRetinaDisplay()
            {
                var dpr = 1;
                if(window.devicePixelRatio !== undefined) dpr = window.devicePixelRatio;
                return dpr >= 2;
            }

            var notRetinaString = "@1x";
            var retinaString = "@2x";

            $(document).ready(function() {
                //if we have a retina display
                if(isRetinaDisplay())
                {
                    //select all elements having background images
                    var $elementsWithBackground = $('*').filter(function() {
                        if (this.currentStyle)
                            return this.currentStyle['backgroundImage'] !== 'none';
                        else if (window.getComputedStyle)
                            return document.defaultView.getComputedStyle(this,null)
                                .getPropertyValue('background-image') !== 'none';
                    });
                    //replace their background-image with retina images
                    $elementsWithBackground.each(function( index ) {
                        $backGroundImage = $(this).css('backgroundImage');
                        /*console.log($backGroundImage);*/
                        //when it is no retina image
                        if($backGroundImage.indexOf(notRetinaString) !== -1)
                        {
                            //replace with retina image
                            $newBackgroundRetinaImage = $backGroundImage.replace(notRetinaString,retinaString);
                            $newBackgroundRetinaImageUrl = $newBackgroundRetinaImage.replace('url("','').replace('")','');
                            //check if retina file exists
                            var $myThis = this;
                            $.ajax({
                                url:$newBackgroundRetinaImageUrl,
                                type:'HEAD',
                                cache: true,
                                success:function() {
                                    console.log($newBackgroundRetinaImage);
                                    $($myThis).css('backgroundImage', $newBackgroundRetinaImage);
                                },
                                error:function() {
                                    console.log("no retina image found for: " + $backGroundImage);
                                }
                            });

                        }
                    });
                }
            });
        </script>

JavaScript check function for retina display

To check, If a device with Retina-Display (new iPhone or Mac Book) surfing on the page, can I use the handy function:

            function isRetinaDisplay()
            {
                var dpr = 1;
                if(window.devicePixelRatio !== undefined) dpr = window.devicePixelRatio;
                return dpr >= 2;
            }

Google Analytics and jQuery mobile

When using Google Analytics with jQuery mobile, there are a few particularities must be observed. So not every page is reloaded with jQuery mobile, but the content (via AJAX) inserted into the existing page and customize the URL, so that creates the impression of an app.

This means for the use of Google Analytics with jQuery mobile, that he should be the best manually counted the pageviews:

To integrate into the <head> The code part of the application:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX']);
/* do not track first page here*/
/*_gaq.push(['_trackPageview']);*/

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

more…