从活动中调用javascript函数? [英] Call a javascript function from the activity?

查看:145
本文介绍了从活动中调用javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用我的活动中的按钮调用加载到webView的HTML文档中的函数?即:ImageButton(称为:bookBtn)在活动的标题栏中。当它被按下/点击时,我需要它来启动webView DOM中的一个功能 - 它基本上将一个新的HTML文档加载到实现幻灯片效果的DOM中。

How can I call a function in a HTML doc that is loaded into a webView with a button that is in my activity? ie: An ImageButton (called: bookBtn) is in the title bar of the activity. When it is pressed/tapped, I need it to fire a function in the webView DOM - It basically loads a new HTML doc into the DOM implementing a slide-in effect.

我发现很多关于如何从webView到活动而不是其他方式执行此操作的文档。 Google表示可以完成,但实际上没有任何示例。我试图调整他们显示的示例代码这里,但我不能让它上班。感谢任何输入。

I found plenty of documentation on how to do this from the webView to the activity but not the other way around. Google says it can be done but there really isnt any examples. I tried to tweek the example code they show here but I cant get it to work. Thank for any input.

以下是加载到webView中的HTML文档中的函数,mWebView:

Below is the function w/in the HTML doc that is loaded into the webView, mWebView:

    <script type="text/javascript">
    $(document).ready(function() {
        function loadTOC(){$.mobile.changePage("docs/1-1.html", "slideup");
        };
    });
</script>

...在我的主要活动中,我按照Google文档进行了尝试, t工作:

...and w/in my main activity, I tried this as per Google docs but doesn't work:

    ImageButton imageButton = (ImageButton) findViewById(R.id.bookBtn);
    imageButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        mHandler.post(new Runnable() {
            public void run() {
                mWebView.loadUrl("javascript:loadTOC");
            }
        });
        }
    });


推荐答案

Thnx为波士顿街道的帮助Google网上论坛指出了正确的方向 - 看到这里

Thnx for the help from 'Streets of Boston' on Google Groups, I was pointed to the right direction - See Here

我在电话&我将功能从头部移动到身体,现在该功能被触发。 :)

I was missing the end/close brackets on the call & I moved the function from my head to the body and now the function fires. :)

这篇关于从活动中调用javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆