开发者

Inject JavaScript file in a WebView on Android

I have a function JavaScript that i want to inject in a Html, with the WebView. I can inject small pieces of JavaScript code with loadUrl("javascript:..." but when i try to insert the code does not work.

Code:

<script type="text/javascript" language="javascript">
    var i = 0;

    function timer() {
        tot = document.links.length;
        if (i < tot+1) {
        document.getElementById('link'+i).focus();
            i++;
            setTimeout("timer()", 1000);
        }
    if(i==tot)
  开发者_运维百科      i=0;
    }
    setTimeout("timer()", 1000);

</script>


Its actually simple, once your webview loading is finshed onPageFinished() , call webview.loadUrl(“javascript:your-javascriptcode-here”) take a look here


Maybe something like:

loadUrl("javascript:(function(){" +
  "var i = 0;" +
  "function timer() {" +
  "    tot = document.links.length;" +
  "    if (i < tot+1) {" +
  "    document.getElementById('link'+i).focus();" +
  "        i++;" +
  "        setTimeout('timer()', 1000);" +
  "    }" +
  "if(i==tot)" +
  "    i=0;" +
  "}" +
  "setTimeout('timer()', 1000);" +
"})()");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜