Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:44:11+00:00 2026-05-30T01:44:11+00:00

I have some HTML5/javascript files hosted on a server. When a button is clicked

  • 0

I have some HTML5/javascript files hosted on a server. When a button is clicked on the HTML5 page a javascript function is invoked. I want to listen for when a function is invoked and get the json that the function returns. Previously I was opening a port on the device but that doesn’t seem to work on Android 3.0. I have heard that you can use external interface to listen to javascript calls but I don’t know how to implement that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:44:11+00:00Added an answer on May 30, 2026 at 1:44 am

    To use external interfaces you need to run your site in WebView. Also the following solution implies, what

    1. you know all the function names, which you want to intercept, and…
    2. those functions are in the public scope of JS code.

    To register your interface you need to call the addJavascriptInterface method on your WebView instance. You need to select a name for it and create an implementation. Your interface should intercept function calls and report their results, so, let’s describe it…

    class FunctionCallInterceptor {
        public void reportCall(String functionName, String result) {
          // some code, handling interception
        }
    }
    

    and register it…

    mWebView.addJavascriptInterface(new FunctionCallInterceptor(), 'Interceptor');
    

    For more information on JavaScript interface see Binding JavaScript

    Then you need to “transfer” function results to your interface… Here you need some JavaScript code.

    function wrapFunc(name) {
      if(typeof window[name] == 'function') {  // If target is accessible
        var original = window['__' + name] = window[name];  // remember original one
        window[name] = function() {  // and replace with wrapper
            var result = original.apply(this, arguments); // call original                
            Interceptor.reportCall(name, result.toString()); // report to interceptor
            return result;  // return result as usual
        }
      }
    }
    

    To wrap your function use this code

    wrapFunc('myFunction'); // wraps myFunction in the source
    

    Also, don’t forget to enable JavaScript at all

    mWebView.getSettings().setJavaScriptEnabled(true);
    

    When, how to embed this code to your external page (I imply what you are not having access to external JS code)… To execute arbitrary code in the page context you can use loadUrl method of WebView

    mWebView.loadUrl('javascript:some... js... code...');
    

    This will not trigger page reload, just JavaScript will be executed. Note what you need to execute this after page is fully loaded. You can obtain this by code below:

    mWebView.setWebViewClient(new WebViewClient {
        @Override
        public void onPageFinished (WebView view, String url) {
            // here page is loaded
        }
    });
    

    See setWebViewClient and onPageFinished for details

    Also note, what code, transferred to page via loadUrl call, must not contain line breaks. So you need to get rid of them (by String.replace or so).

    ADD
    So, the final solution is:

    String wrapFuncCode = "function wrapFunc ...... "; // or maybe place it in resources?
    mWebView.addJavascriptInterface(new FunctionCallInterceptor(), 'Interceptor');
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new WebViewClient {
        @Override
        public void onPageFinished (WebView view, String url) {
           // inject wrapper
           // don't forget to remove newline chars
           mWebView.loadUrl("javascript:" + wrapFuncCode.replace('\n', ''));
    
           // wrap all the functions needed
           String[] funcToWrap = new String[] { 'myFunc1', ... };
           for(String f : funcToWrap) {
               mWebView.loadUrl("javascript:wrapFunc('" + f + "myFunction');");  
           }  
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have page-specific javascript files that look something like: $(document).ready(function () { namespace.home.list
I have a long snippet of HTML I want some javascript variable to equal
I have some html files that I want to convert to text. I have
I have a java web application. Here I have some javascript files which I
I have several javascript files that during run-time get combined and minified. This is
I have a web page that pulls in several javascript files in the HEAD
I have some constants in JavaScript that I'd like to reuse in several files
I have some HTML files, and what I want to do, is put something
I have the following code in JavaScript and jQuery: $(<li />) .html('Some HTML') I
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.