Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript call from WebView to Android and Vice Versa #9

Open
schildroid opened this issue Jan 24, 2013 · 5 comments
Open

Javascript call from WebView to Android and Vice Versa #9

schildroid opened this issue Jan 24, 2013 · 5 comments

Comments

@schildroid
Copy link

Good Day,

I have successfully executed this sample project using Eclipse, but i have this problem... When I add a simple button in the html file provided and attach an event to that button(I would like to call a javascript function), the javascript is not triggering.

Also, I tried to call a javascript function inside the BTWebView class at the OnActionItemClickListener whenever an item is clicked, calling a javascript using this:
this.loadUrl("javascript: testFunction();"); but it's not triggering.

Hope to hear from you Soon.

@btate
Copy link
Owner

btate commented Jan 24, 2013

Wrap your javascript in a try catch block with the error method and see if anything prints.

try{
// Your Code
}
catch(err){
window.TextSelection.jsError(err);
}

That should log an error in eclipse if there's a javascript error somewhere. Good place to start. Aside from that, send me an example of your html content/custom js and I'll try and take a look.

@schildroid
Copy link
Author

Thanks for replying, I have done what you said but still does nothing.

<script type="text/javascript">
        try{
            prom();
        }catch(err){
            window.TextSelection.jsError(err);
        }
        function prom(){
            alert("Function Click!");
        }
</script>

Here's the OnActionItemClickListener with little changes.

mContextMenu.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
            
            public void onItemClick(QuickAction source, int pos,
                int actionId) {
                // TODO Auto-generated method stub
                if (actionId == 1) { 
                    // Do Button 1 stuff
                    Log.i(TAG, "Hit Button 1: ");
                    loadUrl("javascript: prom()"); // Here it is, I'm trying to call a javascript function on the html page provided
                } 
                else if (actionId == 2) { 
                    // Do Button 2 stuff
                    Log.i(TAG, "Hit Button 2");
                } 
                else if (actionId == 3) { 
                    // Do Button 3 stuff
                    Log.i(TAG, "Hit Button 3");
                }
                
                contextMenuVisible = false;
                    
            }
            
        });

No error is being displayed in my Eclipse log cat from a WebView Tag category. Still I did tried pressing the button but the javascript function in my page wont trigger.

Thanks in advance.

@anotherdave
Copy link

I just had this problem too. If you're building for Android API 17 or greater you need to add the @JavascriptInterface annotation to the methods in TextSelectionJavascriptInterface, otherwise they're not available to the webview and the javascript fails with "TypeError: Object [Object] has no method 'setContentWidth'".

@btate
Copy link
Owner

btate commented Aug 26, 2013

Thanks for that. I just updated the class on the master branch with the @JavascriptInterface annotation.

@anotherdave
Copy link

You're welcome, and thanks for the interesting demo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants