2

I was trying to detect the html button click of webview in java code.

My HTML Code:

<button id="fc_complete_order_button" class="fc_button" type="button" value="Complete Your Purchase" onclick="FC.checkout.validateAndSubmit()">Complete Your Purchase</button>
<div id="fc_complete_order_processing" style="display:none;">
    <strong class="fc_error"></strong> <br>
    <img src="//cdn.fox.com/static/v/1.1.0/images/ajax-loader.gif?ver=1" alt="Loading..." width="220" height="19">
</div>

My Java Code is:

final WebView browser = (WebView) findViewById(R.id.browser);

String html="";

try {
    Document doc1 = Jsoup.connect(lastLoadedURL).get();

    html=doc1.toString();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

browser.getSettings().setJavaScriptEnabled(true);

browser.addJavascriptInterface(new Object() {
    @JavascriptInterface
    public void validateAndSubmit() throws Exception {

        Toast.makeText(MainActivity.this, "Login clicked", Toast.LENGTH_LONG).show();
    }
}, "FC.checkout");

Can Anybody suggest where am doing wrong?

1 Answer 1

1

I assume "FC.checkout" is the problem. Google docs told us name the name used to expose the object in JavaScript . In JavaScript "FC.checkout" it's 2 objects: FC and checkout so you are trying to create checkout object inside FC object, but FC object doesn't exist in the browser and I suppose you can catch "object is undefined" exception.

I'm not sure and can't check it right now but I will check and update the answer.

Update: The assumption were correct. Just replace FC.checkout with FC (or anything else that meet JS naming rquirments) in both Java code and HTML

Sign up to request clarification or add additional context in comments.

2 Comments

you are correct in the sense that FC and checkout are the two objects and FC is undefined for the browser. Changing in java code is feasible for me right now but changing the HTML is not feasible, since it is unknown url for me.
So, in that case I suggest to create one object with name FC and then another with FC.checkout. Also you can download entire html as string concat string with definition of FC object and load it into we view via loadData method. You have to define FC method and there are many ways to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.