IOS UIWebView and JS (JavaScript) Interaction-----Hide HTML content based on nodes

Source: Internet
Author: User

In the mobile (this refers to iOS) development, we inevitably use UIWebView and JS interaction.

Let's start with a quick look. Js,javascript is a literal translation of the scripting language, is a dynamic type, weak type, prototype-based language, built-in support type. Widely used in clients, primarily to add interactive behavior to HTML pages. It can be directly embedded in HTML pages, but written as a separate JS file is conducive to the separation of structure and behavior. JS has cross-platform features that can be run on multiple platforms (e.g. Windows, Linux, Mac, Andriod, iOS, etc.) with the support of most browsers. As a literal language, its weaknesses: security.
--Excerpt from Baidu Encyclopedia

Look at the composition of JS:

ECMAScript: Describes the syntax and basic objects of the language

DOM: A Document Object model that describes methods and interfaces for working with Web page content.

BOM: A Browser object model that describes methods and interfaces for interacting with a browser

Example:

Here is the URL of an HTML page

http://breadtrip.com/mobile/destination/topic/2387718902/

Web page Open is this

Use Google browser to open (other also OK), you can see the source code of the site,

I'm just going to graft it right into my webview.

CGRect frame =Self.view.bounds; UIWebView* WebView =[[UIWebView alloc] initwithframe:frame]; WebView.Delegate=Self ; Self.webview=WebView;            [Self.view Addsubview:webview]; dispatch_queue_t Queue= Dispatch_queue_create ("Test", dispatch_queue_serial); Dispatch_async (Queue,^{nsstring* Datastring =[NSString stringwithcontentsofurl:[nsurl URLWITHSTRING:SELF.WEBURL] encoding:nsutf8stringencoding Error:nil]; Datastring= [Datastring stringbyreplacingoccurrencesofstring:@"\"M-app-download\""Withstring:@""]; Datastring= [Datastring stringbyreplacingoccurrencesofstring:@"\"Post-comment\""Withstring:@""]; Dispatch_async (Dispatch_get_main_queue (),^{[Self.webview loadhtmlstring:datastring baseurl:nil];    });    });        Dispatch_release (queue); [WebView release];

But in the development, the information on the page we do not want all, so, can be in the UIWebView proxy method

-(void) Webviewdidfinishload: (UIWebView *) WebView

, using the DOM language to hide the unwanted parts, get the DOM node, and the contents of the operation, there are many ways, here do not repeat, according to demand, here to do hidden operations

- (void) Webviewdidfinishload: (UIWebView *) webview{[Self.webview stringbyevaluatingjavascriptfromstring:@"Document.getelementsbyclassname (\"M-app-download\") [0].hidden = true;"]; [Self.webview stringbyevaluatingjavascriptfromstring:@"Document.getelementsbyclassname (\"Comments\") [0].hidden = true;"]; [Self.webview stringbyevaluatingjavascriptfromstring:@"Document.getelementsbyclassname (\"BTN btn-single\") [0].hidden = True"]; [Self.webview stringbyevaluatingjavascriptfromstring:@"Document.getelementsbyclassname (\"Note\") [0].hidden = True"]; NSString* lengthstring = [Self.webview stringbyevaluatingjavascriptfromstring:@"Document.getelementsbyclassname (\"BTN btn-single\"). Length"]; intLength =[lengthstring Intvalue];  for(inti =0; i<length; i++) {[Self.webview stringbyevaluatingjavascriptfromstring:[nsstring stringWithFormat:@"Document.getelementsbyclassname (\"BTN btn-single\") [%d].hidden = true;", I]]; }    }

This way, you can hide the unwanted parts of the interface.

Reference post: Http://www.cnblogs.com/beijingxiaoguo

Hope to read the blog's friends helpful, if the text is not clear in the description of the place, welcome to leave a message, if there is not written in the place, also please point out

Good night!

Alice

IOS UIWebView and JS (JavaScript) Interaction-----Hide HTML content based on nodes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.