Ruairi O'Brien
Ruairi O'Brien

Reputation: 1229

How to set Javascript properties in Android webview

I am building an Android application that loads a web application in to a web view. When a user visits the web application in a browser a pop up asking if they want to bookmark the page appears.

For the native app I want remove this.

For iPhone I was able to do this:

    [webView stringByEvaluatingJavaScriptFromString:@"AppConfig.showBookmarkPrompt=false;"]; 

For Android I tried this:

@Override
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:(function(){"+
                "AppConfig.showBookmarkPrompt=false;})()");
    }

The Android version doesn't work. I presume that the javascript is being run after the bookmark popup has been shown. I don't really know why the iPhone version does works and not the Android.

There are many other ways I can achieve my goals but I am wondering if anybody can explain why the iPhone method works and if there is something similar in Android. I have been reading about it through Google searches but it is still not entirely clear to me.

Thanks for any help I get.

EDIT

The problem was for Android I was passing a function in JavaScript where I should have just set the property like this: view.loadUrl("javascript:AppConfig.showBookmarkPrompt=false;");

There must be some difference in the way it is executed that I don't quite understand.

Upvotes: 1

Views: 5032

Answers (1)

user370305
user370305

Reputation: 109237

put this on onCreate() of Activity..

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

then call your code...

 @Override
public void onPageFinished(WebView view, String url) {
    view.loadUrl("javascript:AppConfig.showBookmarkPrompt=false");
}

For more info look at here.

Upvotes: 3

Related Questions