Reputation: 1404
i am attempting to use java script to hide a few divs from a webpage, however it doesn't seem to work, can anyone help me, heres my code.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *urlAddress = @"http://tvmdn.org/";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[self.webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function hideID(idName) { "
"var id = document.getElementById(idName);"
"id.style.display = 'none';"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"];
[self.webView stringByEvaluatingJavaScriptFromString:@"hideID('headerbar');"];
}
Thanks, Sami.
Upvotes: 2
Views: 3963
Reputation: 3802
Brendan's answer is correct, but here is a lean twist to the above
- (void)webViewDidFinishLoad:(UIWebView *)webview{
[webView stringByEvaluatingJavaScriptFromString:@"(\".classIdToHide\").hide();"];
}
Upvotes: 0
Reputation: 29986
You shouldn't need to inject the javascript like that (by creating the script element dynamically). You should just be able to do it like this:
Make your class a UIWebViewDelegate
(see: Apple Docs) and just implement the javascript like this:
- (void)webViewDidFinishLoad:(UIWebView *)webview{
NSString *js = @"var element = document.getElementById('headerbar'); element.style.dislay = 'none';";
NSString *res = [webview stringByEvaluatingJavaScriptFromString:js];
[super webViewDidFinishLoad:webview];
}
Upvotes: 2