I am trying to execute some JS before the page onLoad event occurs.
But I am unable to successfully call stringByEvaluatingJavaScriptFromString in the delegate method of webViewDidStartLoad .
To reproduce the problem, you can use the following code.
Delegate Implementation:
-(void)webViewDidStartLoad:(UIWebView *)webView { [webView stringByEvaluatingJavaScriptFromString:@"window.valueHasBeenSet=true"]; }
View this HTML:
<html> <head></head> <body> <script type="text/javascript" charset="utf-8"> if (window.valueHasBeenSet) { </script> <a href="javascript:window.location.reload()">Reload</a> </body> </html>
This page works on the first view ("Everything is OK"), but it does not work on all reboots, regardless of the method of reloading. As you probably expect, this does not work in the shouldStartLoadWithRequest .
I also tried to execute javascript right after webViewDidStartLoad with performSelector:withObject:afterDelay:0 , but to no avail.
Any ideas?
Ed mcmanus
source share