我有一个 Windows Phone 8 项目,我在 WebBrowser
中显示一些数据成分。
WebBworser
Windows Phone 8 中不显示滚动条,因此我自己实现了一个。为此,我需要知道文档的总高度(我使用 document.body.scrollHeight
来获取它)和当前显示的窗口的高度(我使用 document.body.clientHeight
来获取它)。完整的 HTML 模板是
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"/>
<meta name="MobileOptimized" content="width"/>
<meta name="HandheldFriendly" content="true"/>
<script type="text/javascript">
function initialize() {{
window.external.notify("scrollHeight=" + document.body.scrollHeight.toString());
window.external.notify("clientHeight=" + document.body.clientHeight.toString());
window.onscroll = onScroll;
}}
function onScroll(e) {{
var scrollPosition = document.body.scrollTop;
window.external.notify("scrollTop=" + scrollPosition.toString());
}}
window.onload = initialize;
</script>
<style type="text/css">
{0}
</style>
</head>
<body>
{1}
</body>
</html>
到目前为止一切顺利,我的解决方案运行良好。问题是,我想使用 CSS3 所以我必须添加 <!DOCTYPE>
之前<html>
标签以使其工作。
但是添加 <!DOCTYPE>
完全改变了 JavaScript 行为,document.body.scrollHeight
现在不返回整个文档的高度,只是一个略高于 document.body.scrollHeight
的数字。有什么想法可以解决这个问题吗?
最佳答案
要解决 Trident 中的此类奇怪问题,您需要获取文档的高度,而不是正文。
document.documentElement.scrollHeight
是针对文档的完整高度
document.documentElement.clientHeight
是针对文档当前可见部分的高度
PS。请务必不要存储scrollHeight和clientHeight,因为它们可能会由于文档插入或延迟内容加载和/或设备旋转而发生变化
关于javascript - WebBrowser 中具有 Doctype 的不同 JavaScript 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29742457/