删除android webview中的页眉和页脚 [英] removing headers and footers in android webview
问题描述
我正在尝试在 webview 组件中加载响应式网页.但我想去除网页的页眉和页脚,并仅使用网页正文加载 webview.如何在 android webview 中实现这一点.
<header class="header clearfix" ng-include="'/modules/core/homepage/header-partial.html'"></header>
我能够使用 loadDataWithBaseURL() 方法删除页眉和页脚并加载网页
Document document = Jsoup.connect(mUrl).get();document.getElementsByClass("header-container").remove();document.getElementsByClass("footer").remove();WebSettings ws = mWebView.getSettings();ws.setJavaScriptEnabled(true);//mWebView.loadData(document.toString(),"text/html","utf-8");mWebView.loadDataWithBaseURL(mUrl,document.toString(),"text/html","utf-8","");
根据开发者文档:
请注意,JavaScript 的同源策略意味着在使用此方法加载的页面中运行的脚本将无法访问使用除数据"以外的任何方案加载的内容,包括http(s)".为避免此限制,请将 loadDataWithBaseURL() 与适当的基本 URL 结合使用.
http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String, java.lang.String, java.lang.String)
I am trying to load a responsive webpage in a webview component. But I would like to strip the header and footer of the webpage and load the webview with just the body of the webpage. How can this be achieved in an android webview.
<div class="header-container">
<header class="header clearfix" ng-include="'/modules/core/homepage/header-partial.html'"></header>
</div>
I was able to remove the header and footer and load the webpage using loadDataWithBaseURL() method
Document document = Jsoup.connect(mUrl).get();
document.getElementsByClass("header-container").remove();
document.getElementsByClass("footer").remove();
WebSettings ws = mWebView.getSettings();
ws.setJavaScriptEnabled(true);
//mWebView.loadData(document.toString(),"text/html","utf-8");
mWebView.loadDataWithBaseURL(mUrl,document.toString(),"text/html","utf-8","");
As per the developer docs :
Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use loadDataWithBaseURL() with an appropriate base URL.
http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String, java.lang.String, java.lang.String)
这篇关于删除android webview中的页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!