views:

55

answers:

1

I'm trying to execute some JS before the page's onLoad event fires.

But I'm having trouble successfully calling stringByEvaluatingJavaScriptFromString in the webViewDidStartLoad delegate method.

To reproduce the issue, 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)
    {
        // We enter this branch the first time
        document.write('<h3>Everything is OK. Value has been set.</h3>');
    }
    else
    {
        // We incorrectly enter this branch on reloads
        document.write('<h3>Error. Value has not been set.</h3>');
    }
</script>

<a href="javascript:window.location.reload()">Reload</a>

</body>
</html>

This page works on the first view ("Everything is OK.") but fails on all reloads, regardless of the reload method. As you'd probably expect, this doesn't work in the shouldStartLoadWithRequest delegate, either.

I also tried executing the javascript immediately following webViewDidStartLoad with performSelector:withObject:afterDelay:0, to no avail.

Any ideas?

+1  A: 

I managed to make it work using a NSTimer

- (void)viewDidLoad {

timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(update) userInfo:nil repeats:YES]; }

-(void)update{ if (Progress.progress <= 1.0) { [website stringByEvaluatingJavaScriptFromString:@"window.valueHasBeenSet=true"]; } }

chris
I appreciate you taking the time for answering, Chris. However, this doesn't look like a solution. Unless the server is taking a very long time to respond, odds are this timer won't fire when needed. I'm really looking for a deterministic solution, one which isn't dependent on timers.
emcmanus
By the way, this works with an interval of 0. But it's better to create the timer in webViewDidStartLoad and invalidate it in both webViewDidStartLoad and webViewDidEndLoad, than to keep it always running. This also seems to support the theory that webViewDidStartLoad is being called before some cleanup operation, and the window object gets mangled before the web frame gets setup for the next page view.
emcmanus