【问题标题】:Uncaught ReferenceError: myFunction is not defined at null:1 Android exception in webviewUncaught ReferenceError: myFunction is not defined at null:1 Android exception in webview
【发布时间】:2013-07-13 20:25:32
【问题描述】:

我正在从活动中调用 javascript 函数,但我收到 Uncaught ReferenceError: myFunction is not defined at null:1 错误。这是我的文件

MainActivity.java

package com.example.androidexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webview = (WebView)this.findViewById(R.id.webView1);
        WebSettings webSettings = webview.getSettings();

        // Enable Javascript for interaction
        webSettings.setJavaScriptEnabled(true);

        // Make the zoom controls visible
        webSettings.setBuiltInZoomControls(true);

        // Allow for touching selecting/deselecting data series
        webview.requestFocusFromTouch();

        // Set the client
        webview.setWebViewClient(new WebViewClient());
        webview.setWebChromeClient(new WebChromeClient());


        //webview.loadUrl("file:///android_asset/test.html");

        /*String str = "<books>" +
                "<book title=\"A Tale of Two Cities\"/>" +
                "<book title=\"1984\"/>" +
                "</books>";*/




        webview.loadUrl("file:///android_asset/test.html");
        webview.loadUrl("javascript:myFunction()");
        //webview.loadUrl("javascript:myFunction("+str+")");

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }    
}

test.html

<html>
<body>
<script type="text/javascript" src="marknote.js"></script>
<script type="text/javascript">
function myFunction()
{
var str = '<books><book></book></books>';
var parser = new marknote.Parser();
var doc = parser.parse(str);

var bookEls = doc.getRootElement().getChildElements();

    for (var i=0; i<bookEls.length; i++) {
        var bookEl = bookEls[i];

        alert("Element name is " + bookEl.getName());
    }
}
</script>

</body>
</html>

我在使用webview.loadUrl("javascript:myFunction()"); 时看到了这个错误。我想从 java 传递一个 xml 字符串并将其解析为 javascript。请帮助我找到解决方案。

【问题讨论】:

    标签: javascript android webview


    【解决方案1】:

    你应该在页面加载时执行 javascript

    webview.setWebViewClient(new WebViewClient(){
        public void onPageFinished(WebView view, String url){   
            webview.loadUrl("javascript:myFunction()");
        }           
    });
    

    代码将被执行并找到您的 javascript 函数。你现在做的方式不等待。

    【讨论】:

    • 我也试过了,但没有显示错误,但没有调用函数,也没有显示警报消息。
    • 将控制台日志放入 JS 函数并检查发生了什么。另外,为什么要显示来自 for 循环的警报?
    • 这是应该调用JS的方式,错误出在你的maknote库或者你的JS函数的某个部分。
    • @AlexBcn 但是当我在body onload 中调用此函数时,它工作正常
    • body onload 是异步的,将在页面加载时执行。这是与此答案等效的嵌入式 javascript。
    【解决方案2】:

    这个工作只需要改变参数设置顺序

    package com.example.androidexample;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.view.Menu;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final WebView webview = (WebView)this.findViewById(R.id.webView1);
            WebSettings webSettings = webview.getSettings();
    
            // Enable Javascript for interaction
            webSettings.setJavaScriptEnabled(true);
    
            // Make the zoom controls visible
            webSettings.setBuiltInZoomControls(true);
    
            // Allow for touching selecting/deselecting data series
            webview.requestFocusFromTouch();
    
            // Set the client
    //        webview.setWebViewClient(new WebViewClient());
    //        webview.setWebChromeClient(new WebChromeClient());
    
    
    
    
            final String str = "<books>" +
                    "<book title=\"A Tale of Two Cities\"/>" +
                    "<book title=\"1984\"/>" +
                    "</books>";
    
    
    
    //        webview.loadUrl("file:///android_asset/test.html");
    
            webview.setWebChromeClient(new WebChromeClient());
            webview.setWebViewClient(new WebViewClient() 
            {
    
                public void onPageFinished(WebView view, String url)
                {
                    webview.loadUrl("javascript:myFunction('"+str+"')");
                }
            }
                );
            webview.loadUrl("file:///android_asset/test.html");
    
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }    
    }
    

    【讨论】:

    • 不明白。有什么区别?
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多