onPageFinished的Javascript的WebView [英] onPageFinished Javascript WebView

查看:92
本文介绍了onPageFinished的Javascript的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得pretty愚蠢不必问这个,但我不能为我的生命得到它的工作。

所以,我搜索了一圈,发现很多的帮助,指南等使用中的WebViewClient功能onPageFinished和​​我的code主要是基于这些。

基本上我有一个应该载入网页的功能,然后隐藏在onPageFinished该网页上的某些元素。而不是实际这样做虽然它隐藏整个页面和web视图显示文本无。
当我注释掉onPageFinished功能的WebView加载页面就好这使我相信,它是JavaScript,但根据W3C网站我的JavaScript是什么我想要做正确的...

所以我的问题是这样的,有谁看到下面的code和/或任何问题有别的东西,可能会完成我上述目标的建议吗?

在得到这个工作将是极大的AP preciated任何帮助。

code:

 公共类MainActivity扩展AppCompatActivity {    私人的WebView浏览器;
    私人字符串URL =htt​​ps://www.google.com;    <调用的onCreate和cleanpage中其他的东西>    公共无效cleanpage中(){
        浏览器=(的WebView)findViewById(R.id.webview);
        browser.getSettings()setJavaScriptEnabled(真)。
        browser.setWebViewClient(新WebViewClient(){
            @覆盖
            公共无效onPageFinished(的WebView视图,字符串URL){
                browser.loadUrl(JavaScript的:的document.getElementById('LGA')的style.display ='无';);
            }
        });
        browser.loadUrl(URL);
    }
}

注:

以上-The使用谷歌为例,LGA是谷歌的旗帜div的ID。

-browser和url是全球性的,因为一旦我得到cleanpage中工作,我会根据需要,将改变他们的其他功能添加。


解决方案

您需要添加无效(0); 在你的结束 JavaScript的: URL。否则,web视图与JS评估结果将替换当前页面(在你的情况,那就是字符串)。

所以更改此:

  browser.loadUrl(JavaScript的:的document.getElementById('LGA')的style.display ='无';);

<$p$p><$c$c>browser.loadUrl(\"javascript:document.getElementById('lga').style.display='none';void(0);\");

I feel pretty stupid for having to ask this but I can't for the life of me get it to work.

So I've searched around and found lots of help, guides, and etc for using the onPageFinished function in WebViewClient and my code is largely based on these.

Basically I have a function that is supposed to load a web page and then hides certain elements on that page with the onPageFinished. Instead of actually doing this though it hides the entire page and the webview displays the text "none". When I comment out the onPageFinished function the webview loads the page just fine which leads me to believe that it has to be the javascript but according to the w3c site my javascript is correct for what I want to do...

So my question is this, Does anyone see the problem with the below code and/or anyone have a suggestion for something else that might accomplish my above stated goals?

Any help in getting this to work would be greatly appreciated.

Code:

public class MainActivity extends AppCompatActivity{

    private WebView browser;
    private String url = "https://www.google.com";

    < onCreate calls cleanPage and other stuff >

    public void cleanPage() {
        browser = (WebView) findViewById(R.id.webview);
        browser.getSettings().setJavaScriptEnabled(true);
        browser.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                browser.loadUrl("javascript:document.getElementById('lga').style.display='none';");
            }
        });
        browser.loadUrl(url);
    }
}

Notes:

-The above uses google as an example, lga is the id of the google banner div.

-browser and url are global because once I get cleanPage working I will add in other functions that will alter them as needed.

解决方案

You need to add void(0); at the end of your javascript: URL. Otherwise, WebView replaces your current page with the result of the JS evaluation (in your case, that is the string "none").

So change this:

browser.loadUrl("javascript:document.getElementById('lga').style.display='none';");

to:

browser.loadUrl("javascript:document.getElementById('lga').style.display='none';void(0);");

这篇关于onPageFinished的Javascript的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆