Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mathiasbynens/901295 to your computer and use it in GitHub Desktop.
Save mathiasbynens/901295 to your computer and use it in GitHub Desktop.
Improved version of JavaScript fix for the iOS viewport scaling bug. See http://www.blog.highub.com/mobile-2/a-fix-for-iphone-viewport-scale-bug/
// 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";
}
}
}
// Rewritten version
// By @mathias, @cheeaun and @jdalton
(function(doc) {
var addEvent = 'addEventListener',
type = 'gesturestart',
qsa = 'querySelectorAll',
scales = [1, 1],
meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : [];
function fix() {
meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
doc.removeEventListener(type, fix, true);
}
if ((meta = meta[meta.length - 1]) && addEvent in doc) {
fix();
scales = [.25, 1.6];
doc[addEvent](type, fix, true);
}
}(document));
@ShaggyDude
Copy link

Hmm, could you not just pull in the min/max from the actual meta tags and only override them if not present?
That might be best for dumb designers like me.

@matthew-dean
Copy link

This doesn't seem to fix the bug at all. The point of maximum scale is to lock the width to the width of the device. After zooming with this script, when changing orientation, it still has the problem of being too big.

Also, requiring two gestures seems clunky.

@MarkFull
Copy link

MarkFull commented Dec 6, 2011

Any test on iOS 4.3.3?

@atsea
Copy link

atsea commented Dec 8, 2011

Confirmed working on iPad 4.3.5 (8L1) and iPhone 4G 5.0.1
Confirmed NOT working on iPod 5.0

@sergiolopes
Copy link

I think I got a better solution to this bug (just submitted to MBP for evaluation and maybe integration):

https://github.com/sergiolopes/ios-zoom-bug-fix

@mohsen1
Copy link

mohsen1 commented Mar 27, 2012

I face palmed
document.getElementsByTagName('body')[0]

@matthew-dean
Copy link

You mean because of document.body? ;-)

@yahreen
Copy link

yahreen commented Jun 29, 2012

This works great but I've noticed it is adversely affecting my href links.

I have my nav links set to width="100%". If I use my finger to scroll and a link is below my finger, it triggers the :hover state of the link (in this case a background color). I'm assuming it has to do with gesturestart. It happens in both the original script as well as in this improved one.

@davidpaulsson
Copy link

I've heard rumors that this is fixed in iOS6. Can someone confirm this?

@mihaipaun
Copy link

Yes, it's fixed as of iOS6: http://adactio.com/journal/5802/

@AlecRust
Copy link

What about this CSS-only solution? http://stackoverflow.com/a/8727440/312681

@AlecRust
Copy link

@mihaipaun not as far as I can tell...

@benjamincharity
Copy link

If that bug was fixed there should be some release information on it somewhere. I'd love to see a link on this to know if it truly is supposed to be fixed. I too have heard the rumors.

I read through what I could find on the iOS 6 release with no luck.

@tawasul-steel-hardware-trdgllc

http://mathiasbynens.be/demo/ios-viewport-scaling

In order for the script to work and bypass the 2nd gesture minor tweaking like in orienatation change event set the max to 1.00099 instead of just 1.0

@sila1
Copy link

sila1 commented Jul 3, 2013

hi, i'm still in a learning process and really grateful to all sites like this one!

the code works fine with me. the only thing is that a tiny part of the page is being cut off from the sides. is there a way of preventing this or is it possibly a self-inflicted problem because i made my page 1000px wide?

i'd be very grateful to any help and ideas!

@ddantes
Copy link

ddantes commented Jul 12, 2013

I appreciate this code, which has helped me as I've adapted my website to be more mobile friendly. I'm posting about an issue which just came to my attention, hoping for some advice. It seems that the bug fix doesn't work if a page includes dynamic text. I have two pages which implement the bug fix, although the script is external in a file named MaintainViewport.js. If you visit http://mauitradewinds.com/test/view.htm you will see a horizontally-scrolling image with a fixed text caption. The viewport bug fix works perfectly. But, at http://mauitradewinds.com/test/test.htm there is a fading caption below the scrolling image. The viewport bug is not fixed on this page, although it references the same javascript to correct that bug. If I remove the dynamic caption, the viewport bug also disappears. If there is a solution to this, I'd be grateful to see it.

@highruned
Copy link

}(document));? should be })(document);

@enure
Copy link

enure commented Feb 10, 2014

@ericmuyser - you can write it either way. The latter is more common, but the former works as well.

@jpblancoder
Copy link

You might want to update with additional meta params, for iOS 6+.

function fix() {
        meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=' + scales[1] + ', user-scalable=no, minimum-scale=' + scales[0];
        doc.removeEventListener(type, fix, true);
    }

@DaveFlash
Copy link

hmm, still seems to create problems for me....

@HarroH
Copy link

HarroH commented Aug 11, 2017

Is this fix still applicable to iOS or has Apple solved this issue?

@webaffin
Copy link

Is this fix still applicable to iOS or has Apple solved this issue?

I also want to know...

@GraniteConsultingReviews

I tested this code and its working well. thanks for sharing.

@MattShaile
Copy link

I don't think this works any more (iOS 10+) due to Apple now ignoring the scale meta tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment