Sto provando a eseguire alcuni JS prima che l'evento onLoad della pagina venga attivato.JavaScript interrotto nella callback "Start Load" di UIWebView
Ma ho problemi con successo chiamando stringByEvaluatingJavaScriptFromString
nel metodo delegato webViewDidStartLoad
.
Per riprodurre il problema, è possibile utilizzare il seguente codice. implementazione
Delegato:
-(void)webViewDidStartLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:@"window.valueHasBeenSet=true"];
}
Visualizza questa 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>
Questa pagina funziona sulla prima vista ("Tutto è OK"), ma non su tutte le ricariche, indipendentemente dal metodo di ricarica. Come probabilmente ti aspetti, anche questo non funziona nel delegato shouldStartLoadWithRequest
.
Ho anche provato ad eseguire il javascript immediatamente dopo webViewDidStartLoad
con performSelector:withObject:afterDelay:0
, senza risultato.
Qualche idea?
Qualche soluzione per questo? – Tudorizer
Sì, vedi il mio commento qui sotto. –