Skip to content

Commit

Permalink
Added check for if source has already been updated
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbilson committed Oct 29, 2011
1 parent a602b7f commit c4e45c1
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jquery.retina.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
1.0 (23/08/2010) - Initial release.
1.1 (27/08/2010) - Made plugin chainable
1.2 (10/11/2010) - Fixed broken retina_part setting. Wrapped in self executing function (closure)
1.3 (29/10/2011) - Checked if source has already been updated
*/

(function( $ ){
Expand All @@ -20,6 +21,9 @@
if(window.devicePixelRatio >= 2) {
this.each(function(index, element) {
if(!$(element).attr('src')) return;

var checkForRetina = new RegExp("(.+)("+settings['retina_part']+"\\.\\w{3,4})");
if(checkForRetina.test($(element).attr('src'))) return;

var new_image_src = $(element).attr('src').replace(/(.+)(\.\w{3,4})$/, "$1"+ settings['retina_part'] +"$2");
$.ajax({url: new_image_src, type: "HEAD", success: function() {
Expand Down

1 comment on commit c4e45c1

@mattbilson
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phonegap (and maybe other mobile app frameworks) must handle ajax HEAD requests strangely, as in my app if retina() was called numerous times (after adding content to the page via ajax for example), the image src would become example-2x-2x.jpg

Even without the Phonegap issue, if the retina() function is called numerous times, this reduces the number of ajax requests which are made to check if the image exists.
It does mean that a normal, non-retina image (which needs switching), can't be named with -2x. However this seems rare, and if needed, retina_part can be changed accordingly.

Please sign in to comment.