Reputation: 359
I want to use javascript to check if a class exists within a UIWebView. This is what I have so far:
NSString* checkForWaldoCmd = [[NSString alloc] initWithString:@"document.getElementsByClassName('waldo');"];
NSString* wheresWaldo = [myUIWebView stringByEvaluatingJavaScriptFromString:checkForWaldoCmd];
I basically want someway to check if the class 'waldo' exists within the page. When I run the code above, I get a blank string whether the class exists or not. Any suggestions?
Upvotes: 1
Views: 961
Reputation: 8759
The main trick to using stringByEvaluatingJavaScriptFromString: is to evaluate expressions that evaluate to a type can easily be converted to string (native simple types usually work: float, int and string). In your example, @"document.getElementsByClassName('waldo');" will be of type NodeList that has no simple string representation and that is why you get an empty string. Try, for example, getting the length of the list of elements with class waldo:
NSString *count = [myUIWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('waldo').length;"];
if ([count intValue] > 0){
NSLog(@"Have elements of class waldo");
}else{
NSLog(@"Don't have elements of class waldo");
}
Upvotes: 5