Created
February 10, 2014 17:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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