Skip to content

Instantly share code, notes, and snippets.

@RavisMsk
Created August 29, 2013 12:45
Show Gist options
  • Save RavisMsk/6377617 to your computer and use it in GitHub Desktop.
Save RavisMsk/6377617 to your computer and use it in GitHub Desktop.
Scale UIWebView contents to view width using javascript
[_webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:@"var result = '';"
"var viewport = null;"
"var content = null;"
"var document_head = document.getElementsByTagName( 'head' )[0];"
"var child = document_head.firstChild;"
"while ( child )"
"{"
" if ( null == viewport && child.nodeType == 1 && child.nodeName == 'META' && child.getAttribute( 'name' ) == 'viewport' )"
" {"
" viewport = child;"
" viewport.setAttribute( 'content' , 'width=270;initial-scale=1.0' );"
" result = 'fixed meta tag';"
" }"
" child = child.nextSibling;"
"}"
"if (null == viewport)"
"{"
" var meta = document.createElement( 'meta' );"
" meta.setAttribute( 'name' , 'viewport' );"
" meta.setAttribute( 'content' , 'width=device-width;initial-scale=1.0' );"
" document_head.appendChild( meta );"
" result = 'added meta tag';"
"}"
]
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment