6

I'm building an app which load a webpage in a webview. In that webpage, i need to programmatically click on some links using Jquery. Now, i know how to execute a Javascript code on the webview programmatically (see below):

WebSettings myBrowserSettings = myBrowser.getSettings();
myBrowserSettings.setJavaScriptEnabled(true);
Log.d("Stefano", "JS enabled");    


myBrowser.loadUrl("javascript:document.getElementsByid('myWord').click();");

But now, I need to know how implement a Jquery function in my webview; i'm looking for the correct way to manage something like:

myBrowser.loadUrl("jquery:function($("#myAnchor").click(function(event){})");

And which is the correct way to implement the following function?

$("#a_link")[0].click();
1
  • If the original question you did was resolved by any of the posted answers, you should mark one as accepted. And if you have any other question, you should open a new one. By the way, the code $("#a_link")[0].click(); seems fine. What problem are you having? Commented Sep 4, 2018 at 10:20

2 Answers 2

5

If jquery loaded in this page, you can just call this:

webview.setWebViewClient(new WebViewClient() {  
    @Override  
    public void onPageFinished(WebView view, String url) {  
        webview.loadUrl("javascript:(function() { " +  
                    "$("#myAnchor").click(function(event){}" +  
                    "})()");  
    }  
});
Sign up to request clarification or add additional context in comments.

2 Comments

I can't modify the HTML code of the pages because i connect to websites which are not mine. So, loading a webpage from the web, how can i use Jquery?
Got it, I changed my answer
3

What's your problem? It's about escaping characters?

myBrowser.loadUrl("jquery:function($(\"#myAnchor\").click(function(event){})");

Comments

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.