Skip to content

Instantly share code, notes, and snippets.

@lifedraft
Created February 27, 2014 13:55
Show Gist options
  • Save lifedraft/9250476 to your computer and use it in GitHub Desktop.
Save lifedraft/9250476 to your computer and use it in GitHub Desktop.
Update meta viewport tag's content for fit page width. ios and android safe.
viewport = document.getElementById('viewport')
orientationchange = (event) ->
unit = "width"
current_orientation = window.orientation
wider = window.screen.width > window.screen.height
if (current_orientation == 180 and wider) or
(current_orientation == 0 and wider) or
(current_orientation == -90 and !wider) or
(current_orientation == 90 and !wider)
unit = "height"
unit = window.screen[unit]
if (unit > 700)
ratio = unit/1032
viewport.setAttribute('content', 'width=1032, initial-scale='+ratio+', maximum-scale=' +ratio + ', user-scalable=yes')
else
viewport.setAttribute 'content', 'width=device-width, initial-scale=1.0, user-scalable=no'
$(window).bind 'orientationchange', orientationchange
orientationchange()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment