简体   繁体   English

Javascript在Android上不起作用

[英]Javascript doesn't work on Android

I have a very basic app that just load JavaScript to a webview. 我有一个非常基本的应用程序,只需将JavaScript加载到webview。

my JavaScript file is this 我的JavaScript文件就是这个

This is my code : 这是我的代码:

public class MainActivity extends Activity {
WebView myBrowser;
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_main);

    myBrowser = (WebView)findViewById(R.id.mybrowser);

    final MyJavaScriptInterface myJavaScriptInterface
    = new MyJavaScriptInterface(this);
    myBrowser.getSettings().setJavaScriptEnabled(true); 
    myBrowser.addJavascriptInterface(new Object(), "AndroidFunction");
    myBrowser.setWebChromeClient(new WebChromeClient());
    myBrowser.loadUrl("file:///android_asset/sperm.html");
}

public class MyJavaScriptInterface {
      Context mContext;

         MyJavaScriptInterface(Context c) {
             mContext = c;
         }

         public void showToast(String toast){
             Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
         }

         public void openAndroidDialog(){
          AlertDialog.Builder myDialog
          = new AlertDialog.Builder(MainActivity.this);
          myDialog.setTitle("DANGER!");
          myDialog.setMessage("You can do what you want!");
          myDialog.setPositiveButton("ON", null);
          myDialog.show();
         }

     }

And this is the XML file : 这是XML文件:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
 android:background="@drawable/background">
<WebView
   android:id="@+id/mybrowser"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
  />

As I said this is very basic app. 正如我所说,这是非常基本的应用程序。

The style in the html file working good, but the script doesn't run. html文件中的样式运行良好,但脚本不运行。

I'm new with Javascript so any direction could help. 我是Javascript的新手,所以任何方向都可以提供帮助。

Thanks! 谢谢!

EDIT 1 : 编辑1:

I take the java code for Android from THIS guide 我从THIS指南中获取了Android的Java代码

There is no connection between the Java code and the HTML file, so Myabe there is a need to change MyJavaScriptInterface class. Java代码和HTML文件之间没有任何联系,所以Myabe需要更改MyJavaScriptInterface类。

Sorry if this confused. 对不起,如果这困惑。

check your "sperm.html", it include js file : 检查你的“sperm.html”,它包含js文件:

<script src="http://d3js.org/d3.v3.min.js"></script>

solution 1 : 解决方案1:
it use inertent when load js, so you have add uses-permission in Android manifest : 它在加载js时使用惯性,因此您在Android清单中添加了uses-permission:

<uses-permission android:name="android.permission.INTERNET" />


solution 2: 解决方案2:
download "d3.v3.min.js" and put it in assets folder, then modify "sperm.html" 下载“d3.v3.min.js”并将其放入assets文件夹,然后修改“sperm.html”

<script src="d3.v3.min.js"></script>

Replace: 更换:

<script>

with

 <script type="text/javascript">

In your HTML file. 在您的HTML文件中。

Also set the Webchromeclient first, then enable javascript: 还要先设置Webchromeclient,然后启用javascript:

myBrowser.setWebChromeClient(new WebChromeClient());
myBrowser.getSettings().setJavaScriptEnabled(true); 

The javascript interface is not needed to make the javascript work, it is used to call android functions from the javascript in the webview. javascript界面​​不需要使javascript工作,它用于从webview中的javascript调用android函数。 To make this work you need to provide addJavascriptInterface with the interface you made. 要完成这项工作,您需要为您创建的接口提供addJavascriptInterface。

Example: 例:

myBrowser.addJavascriptInterface(new MyJavaScriptInterface(this), "AndroidFunction");

If that doesn't work, can you add the javascript code? 如果这不起作用,你可以添加javascript代码吗?

Try this: 尝试这个:

 import android.webkit.JavascriptInterface;
  @JavascriptInterface
    public void MyJavaScriptInterface {

OR 要么

@android.webkit.JavascriptInterface
public void MyJavaScriptInterface {

You need to add this annotation to your user-defined javascript interface after API level 16. 您需要在API级别16之后将此批注添加到用户定义的javascript界面​​。

try to add WebChromeClient to your webview and enable javascript 尝试将WebChromeClient添加到您的webview并启用javascript

webView.setWebChromeClient(new WebChromeClient());

webView.getSettings().setJavaScriptEnabled(true); webView.getSettings()setJavaScriptEnabled(真)。 webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 。webView.getSettings()setJavaScriptCanOpenWindowsAutomatically(真);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM