UIWebView often encountered with the use of JS to deal with the things, and today encountered a search, find this article feel good to cherish a bit.
Original address http://jiapumin.iteye.com/blog/1558345 thank the original author Jiapumin demand: Mixed application UIWebView open HTML, UIWebView have left and right scroll bar, to remove the left and right scrolling effect;
Methods: Using JS to intercept the HTML in UIWebView, and then modify the contents of HTML tags;
Instance code:
Server-side HTML
-
- <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
- <meta name="viewport" content="Width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale= 1.0, User-scalable=no ">
- <title> network exposure to a test room clock slow half hour teacher said this is life </title>
The minimum width of the resulting web page will be device-width, but sometimes it is not necessary to modify the width=device-width to Width=mywidth;
Client code
- -(void) Webviewdidfinishload: (UIWebView *) WebView
- {
- //Modify the meta value of the server page
- NSString *meta = [NSString stringwithformat:@"Document.getelementsbyname (\" viewport\ ") [0].content = \" Width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\ "", WebView.frame.size.width];
- [WebView Stringbyevaluatingjavascriptfromstring:meta];
- }
So the problem can be solved.
New Code:
- Add utf-8 encoding to Web pages
- [WebView stringbyevaluatingjavascriptfromstring:
- @"var taghead =document.documentelement.firstchild;"
- "var tagmeta = document.createelement (\" Meta\ ");"
- "Tagmeta.setattribute (\" http-equiv\ ", \" content-type\ ");"
- "Tagmeta.setattribute (\" content\ ", \" text/html; charset=utf-8\ ");"
- "var tagheadadd = Taghead.appendchild (Tagmeta);"];
- Add CSS styles to a Web page
- [WebView stringbyevaluatingjavascriptfromstring:
- @"var taghead =document.documentelement.firstchild;"
- "var tagstyle = document.createelement (\" style\ ");"
- "Tagstyle.setattribute (\" type\ ", \" Text/css\ ");"
- "Tagstyle.appendchild (document.createTextNode (\" body{padding:20pt 15pt}\ "));"
- "var tagheadadd = Taghead.appendchild (Tagstyle);"];
- Block page images and modify image size
- [WebView stringbyevaluatingjavascriptfromstring:
- @"var script = document.createelement (' script ');"
- "Script.type = ' text/javascript ';"
- "script.text = \" Function resizeimages () {"
- "var myimg,oldwidth;"
- "var maxwidth=380;" //Zoom factor
- "for (I=0;i <document.images.length;i++) {"
- "myimg = Document.images[i];"
- "if (Myimg.width > MaxWidth) {"
- "oldwidth = Myimg.width;"
- "myimg.width = MaxWidth;"
- "Myimg.height = Myimg.height * (maxwidth/oldwidth);"
- "}"
- "}"
- "}\";"
- "document.getElementsByTagName (' head ') [0].appendchild (script);"];
- [WebView stringbyevaluatingjavascriptfromstring:@"resizeimages ();"];
Other HTML property overloads are similar to this method;
Reference URL:
(How to use stringbyevaluatingjavascriptfromstring) http://www.uml.org.cn/mobiledev/201108181.asp
(iphone gets UIWebView inside HTML method) http://blog.csdn.net/diyagoanyhacker/article/details/6564897
(IOS UIWebView referencing external CSS styles) http://hi.baidu.com/jwq359699768/item/780879e5c98bfb3e4ddcaf22
http://blog.csdn.net/xdonx/article/details/6973521
IOS UIWebView intercepts HTML and modifies note content