开发者

How to set Javascript properties in Android webview

开发者 https://www.devze.com 2023-04-05 14:20 出处:网络
I am building an Android application that loads a web application in to a web view. When a user vi开发者_StackOverflow中文版sits the web application in a browser a pop up asking if they want to bookma

I am building an Android application that loads a web application in to a web view. When a user vi开发者_StackOverflow中文版sits 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消