简体   繁体   中英

my JS fails to get contents of a div in Objective-C

I am managing to retrieve some text from my Webview. Here is a example of what my HTML file looks like.

Template.html:

<html>
<body>
        <div id="content" contenteditable="true" style="font-family: Helvetica">
            hello
            </div>
</body>
</html>

(The code above is exactly all I have in my template.html file.)

Then I run:

NSString *js = @"document.getElementById('content').innerHTML";
NSString* html = [panel stringByEvaluatingJavaScriptFromString:js];

It returns NOTHING! Is Xcode stopping to support this type of JS?

Please help, thanks a lot!

---------EDIT------------ For more information, this is how I load my page:

NSBundle *bundle = [NSBundle mainBundle];
NSURL* temp = [bundle URLForResource:@"template" withExtension:@"html"];
[panel loadRequest:[NSURLRequest requestWithURL:temp]];

Loaded UIWebView content to be fetch using below line

NSString *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];

Source: More - details- stackoverflow

Thanks

What I discovered at last is in fact an issue of UIWebViewDelegate!

-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *js = @"getContent();";//This function is used to get 
                                //the innerHTML of the document.
NSString* html = [panel stringByEvaluatingJavaScriptFromString:js];
L22(@"content HTML:",html);
}

This will give me the text within my div.

Always make sure the website is fully loaded before extracting any information out of it!:0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM