Forked from mathiasbynens/ios-viewport-scaling-bug-fix-original.js
Created
August 23, 2013 20:04
-
-
Save honne/6323438 to your computer and use it in GitHub Desktop.
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
| // Original code from http://www.blog.highub.com/mobile-2/a-fix-for-iphone-viewport-scale-bug/ | |
| var metas = document.getElementsByTagName('meta'); | |
| var i; | |
| if (navigator.userAgent.match(/iPhone/i)) { | |
| for (i=0; i<metas.length; i++) { | |
| if (metas[i].name == "viewport") { | |
| metas[i].content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0"; | |
| } | |
| } | |
| document.getElementsByTagName('body')[0].addEventListener("gesturestart", gestureStart, false); | |
| } | |
| function gestureStart() { | |
| for (i=0; i<metas.length; i++) { | |
| if (metas[i].name == "viewport") { | |
| metas[i].content = "width=device-width, minimum-scale=0.25, maximum-scale=1.6"; | |
| } | |
| } | |
| } |
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
| // My rewritten version | |
| (function(doc) { | |
| var metas = doc.querySelectorAll('meta[name="viewport"]'), | |
| forEach = [].forEach; | |
| function fixMetas(isFirstTime) { | |
| var scales = isFirstTime ? ['1.0', '1.0'] : ['0.25', '1.6']; | |
| forEach.call(metas, function(el) { | |
| el.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1]; | |
| }); | |
| } | |
| fixMetas(true); | |
| doc.body.addEventListener('gesturestart', fixMetas, false); | |
| }(document)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment