从Android调用JavaScript函数不起作用

时间:2013-09-26 07:52:34

标签: javascript android

我有一个Webview,其中加载了用户定义的网站,然后在按下WebView下方的按钮时,Javascript(来自远程URL)被加载到用户所在的页面中。

然后应该触发此JavaScript中的一个函数,但它似乎不起作用。

我的活动:

public class SearchViewer extends SherlockActivity {

    TextView grabit;
    WebView webView;
    ImageView right, left;
    ProgressBar progressBar;

    @SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchviewer);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_arrowleft);

        Intent intent = getIntent();
        String url = intent.getStringExtra("URL");
        String name = intent.getStringExtra("Name");
        setTitle(name);

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        webView = (WebView) findViewById(R.id.webView1);
        webView.loadUrl(url);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }

            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);

                progressBar.setVisibility(View.GONE);

            }
        });
        grabit = (TextView) findViewById(R.id.grabit);
        grabit.setOnClickListener(OnClick_grabit);

        right = (ImageView) findViewById(R.id.right);
        left = (ImageView) findViewById(R.id.left);

        right.setOnClickListener(OnClick_right);
        left.setOnClickListener(OnClick_left);

    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case android.R.id.home:
            finish();
            break;

        default:
            return super.onOptionsItemSelected(item);
        }
        return false;
    }

    public void onStart() {
        super.onStart();
        EasyTracker.getInstance(this).activityStart(this);
    }

    public void onStop() {
        super.onStop();
        EasyTracker.getInstance(this).activityStop(this);
    }

    private OnClickListener OnClick_grabit = new OnClickListener() {
        public void onClick(View v) {
            webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");
            webView.loadUrl("javascript: initialize("+1+")");
        }
    };
    private OnClickListener OnClick_right = new OnClickListener() {
        public void onClick(View v) {
            webView.goForward();
        }
    };
    private OnClickListener OnClick_left = new OnClickListener() {
        public void onClick(View v) {
            webView.goBack();
        }
    };

当我用

加载javascript时
webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");

并在Javascript文件中启动Javascript initialize()函数,它可以正常工作。 但是,当我尝试做上述两行时

webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");
                webView.loadUrl("javascript: initialize("+1+")");

它不起作用,我怎么能加载Javascript然后从android启动函数,所以我可以传递一个值。

我试过了

private OnClickListener OnClick_grabit = new OnClickListener() {
        public void onClick(View v) {
            webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pmdev.co.uk/dadg-giftgrabber/android/gift-grabber.js');");
            webView.setWebViewClient(new WebViewClient(){

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    webView.loadUrl("javascript: initialize("+1+")");
                }

            });
        }
    };

但这不起作用

2 个答案:

答案 0 :(得分:0)

你试过这个:

webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');initialize("+1+")");

答案 1 :(得分:0)

Lajos的答案应该有效,但是,如果你想让你的最后一个片段工作,你需要在调用初始化的onPageFinished之前调用setWebViewClient,然后再实际调用加载了loadUrl的{​​{1}}脚本,即使你这样做,你应该小心,因为你要两次调用你的javascript initialize()函数。 问题是,loadUrl在加载完成后会产生对onPageFinished的调用(即使url是javascript: url。