在Webview中运行javascript代码

时间:2015-08-23 05:33:42

标签: javascript android webview

我有一个我在android中使用的webview,我试图在按钮点击时触发javascript。我尝试使用下面的代码将类的颜色更改为红色。但我似乎无法让它运作

final WebView wb=(WebView)findViewById(R.id.webView2);
wb.loadUrl("javascript:"
                + "var FunctionOne = function () {"
                + "  try{document.getElementsByClassName('test')[0].style.color='red';}catch(e){}"
                + "};");

3 个答案:

答案 0 :(得分:20)

从kitkat开始,使用evaluateJavascript方法而不是loadUrl来调用下面的javascript函数

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        webView.evaluateJavascript("var FunctionOne = function () {"
            + "  try{document.getElementsByClassName('test')[0].style.color='red';}catch(e){}"
            + "};", null);
    } else {
        webView.loadUrl("javascript:"
            + "var FunctionOne = function () {"
            + "  try{document.getElementsByClassName('test')[0].style.color='red';}catch(e){}"
            + "};");
    }

通过添加以下行

为您的网页浏览启用Javascript
wb.getSettings().setJavaScriptEnabled(true);

答案 1 :(得分:0)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.bluapp.androidview.R;

public class WebViewActivity3 extends AppCompatActivity {
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view3);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/webview1.html");


        webView.setWebViewClient(new WebViewClient(){
            public void onPageFinished(WebView view, String weburl){
                webView.loadUrl("javascript:testEcho('Javascript function in webview')");
            }
        });
    }
}

assets / webview1.html

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>WebView1</title>
<meta forua="true" http-equiv="Cache-Control" content="max-age=0"/>
</head>

<body style="background-color:#212121">
<script type="text/javascript">
function testEcho(p1){
document.write(p1);
}
</script>
</body>

</html>

答案 2 :(得分:-2)

使用以下方式在Webview中启用JavaScript:

final WebView wb=(WebView)findViewById(R.id.webView2);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);