将Javascript注入WebView赢得';有时我不工作?
我正在使用将Javascript注入WebView赢得';有时我不工作?,javascript,android,webview,Javascript,Android,Webview,我正在使用WebView播放一些html5动画,这些动画依赖于一些javascript代码。在加载网页之前,我需要将javascript代码注入WebView class AnimWebView extends WebView{ getSettings().setJavaScriptEnabled(true); loadAnimation(String animUrl){ //get javascript code String js_code
WebView
播放一些html5动画,这些动画依赖于一些javascript
代码。在加载网页之前,我需要将javascript
代码注入WebView
class AnimWebView extends WebView{
getSettings().setJavaScriptEnabled(true);
loadAnimation(String animUrl){
//get javascript code
String js_code = getMyJsCode();
//inject the javascript code to WebView
mWebView.loadUrl("javascript:" + js_code);
//load animation
mWebView.loadUrl(animUrl);
}
}
AnimWebView animContainer = new AnimWebView(context);
// load first animation, the animation works fine.
animContainer.loadAnimation("file:///android_asset/animations/test1/index.html");
//after one minute load second animation(I omit the delay code.)
//it doesn't work because JS code can't be find.
animContainer.loadAnimation("file:///android_asset/animations/test2/index.html");
因为它第一次工作,第二次失败,所以我想第一次之后可能需要清除代码和缓存
所以我试着clearView()
,stopLoading()
,loadUrl(“about:blank”)
。但它们都不起作用
有什么建议吗?您需要添加mWebView.getSettings().setJavascriptEnabled(true)Hi-asgoth,我已经添加了,不起作用。核心问题是webview中的内容第一次运行良好,但第二次失败。