Skip to content

Instantly share code, notes, and snippets.

@jornki
Created February 10, 2014 17:11
Show Gist options
  • Select an option

  • Save jornki/8920123 to your computer and use it in GitHub Desktop.

Select an option

Save jornki/8920123 to your computer and use it in GitHub Desktop.
A simple polyfill for enabling the «img srcset 2x» attribute in browsers which does not yet support it
SrcSetSimplePolyfill =
init : () ->
img = new Image()
if 'srcset' of img is no and (window.devicePixelRatio || 1) > 1
images = document.querySelectorAll('img');
for image in images
@swapImagSrc image
swapImagSrc : (image) ->
comp = @parseURL image
if comp
image.src = comp
parseURL : (url) ->
set = url.getAttribute 'srcset'
exp = new RegExp /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~,#?&//=]*)?/gi
set.match(exp)[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment