Javascript 使用Javascript获取webView内容的总高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2979854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get total height of webView's content using Javascript
提问by Emil
I'm using a UIWebViewfor displaying content, in the form of an HTML string – not a website, higher than the screen of the iPhone, without needing to scroll in the webView itself, leaving that to the parent scrollView.
我使用 aUIWebView以 HTML 字符串的形式显示内容 - 不是网站,高于 iPhone 的屏幕,无需在 webView 本身中滚动,将其留给父 scrollView。
To achieve this, I need a way to get the total document size, including the scrollable area, to set the webView's height. I have tried a number of different Javascript solutions:
为了实现这一点,我需要一种方法来获取总文档大小,包括可滚动区域,以设置 webView 的高度。我尝试了许多不同的 Javascript 解决方案:
(document.height !== undefined) ? document.height : document.body.offsetHeight // Returns height of UIWebView
document.body.offsetHeight // Returns zero
document.body.clientHeight // Returns zero
document.documentElement.clientHeight // Returns height of UIWebView
window.innerHeight // Returns height of UIWebView -2
document.body.scrollHeight // Returns zero
Is there a solution that actually works?
有没有实际可行的解决方案?
Current (nonworking) code:
当前(非工作)代码:
[[[self.singlePost.contentText subviews] lastObject] setScrollEnabled:NO];
int content_height = [[self.singlePost.contentText stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue];
NSLog(@"Content_height: %d", content_height);
CGRect rect = self.singlePost.contentText.frame;
rect.size.height = content_height;
self.singlePost.contentText.frame = rect;
回答by Andres Kievsky
There is no need to use Javascript in iOS 5.0 and up - you have direct, documented access to its scrollView:
在 iOS 5.0 及更高版本中无需使用 Javascript - 您可以直接、记录在案的访问其滚动视图:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat contentHeight = webView.scrollView.contentSize.height;
// ....
}
To get the total height of the contents of the webView.
获取 webView 内容的总高度。
回答by karim
When I tried with my code, I found,
当我尝试使用我的代码时,我发现,
(1) NSLog(@"webView height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]);
(2) NSLog(@"webView height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.height"]);
(1)return the height less than the original height. But (2)return the actual height. My suggestion is to get the Maxof the two.
(1)返回小于原始高度的高度。但(2)返回实际高度。我的建议是得到Max两者中的。
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSLog(@"Body height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]);
NSLog(@"Doc height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.height"]);
NSString * javaScript = [NSString stringWithFormat:@"document.getElementById('%@').clientHeight", kDivID];
NSLog(@"Div height: %@",[webView stringByEvaluatingJavaScriptFromString:javaScript]);
[self reLayout];
}
- (CGFloat) getWebViewPageHeight {
CGFloat height1 = [[webView stringByEvaluatingJavaScriptFromString: @"document.height"] floatValue];
CGFloat height2 = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] floatValue];
return MAX(height1, height2);
}
Output
输出
Body height: 485
Doc height: 509
Div height: 485
回答by robert
Where do you call your code? For me it returns 0 if it is called right after the loadHTMLString Method. If I call it in the (void)webViewDidFinishLoad:(UIWebView *)theWebView delegate, I get a valid value.
你在哪里调用你的代码?对我来说,如果在 loadHTMLString 方法之后立即调用它,它会返回 0。如果我在 (void)webViewDidFinishLoad:(UIWebView *)theWebView 委托中调用它,我会得到一个有效值。
- (void)loadHTML: (NSString *)html {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
webView.delegate = self;
NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL: resourceURL];
NSLog(@"height: %d", [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue]); // returns 0
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSLog(@"height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]); //return 2166
}
回答by t9mike
I went with a slightly different approach, to create a <DIV> that I could key off of to get the size, because I was having no luck with inspecting document/body.
我采用了一种稍微不同的方法,创建了一个 <DIV>,我可以关闭它以获得大小,因为我在检查文档/正文时没有运气。
This is MonoTouch C#, but should work fine in Objective-C:
这是 MonoTouch C#,但在 Objective-C 中应该可以正常工作:
private const string DIV_ID = "_uiwebview_";
LoadHtmlString("<body style='background-color:#yourcolor; padding:0px; margin:0px'>" +
"<div style='width:" + Frame.Width + "px; padding:0px; margin:0px; font-family:" +
font.Name + "' id=" + DIV_ID + ">" + html + "</div></body>", base_url);
and getting the height in LoadFinished:
并在 LoadFinished 中获取高度:
string sheight = EvaluateJavascript("document.getElementById('" + DIV_ID +
"').clientHeight");
@Jesse, this works when updating with HTML that would generate a smaller size than was previously shown.
@Jesse,这在使用 HTML 更新时有效,该 HTML 会生成比以前显示的尺寸更小的尺寸。

