javascript - WebBrowser 中具有 Doctype 的不同 JavaScript 行为

标签 javascript c# xaml windows-phone-8

我有一个 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/

相关文章:

javascript - 编码替代阴影行?

c# - 如何获取存储在内容文件夹.net 中的图像的路径

c# - : Bind to a UserControl's DependencyProperty, 当 UserControl 有一个 DataContext 时如何?

javascript - 如何使用 jquery 创建图像缩放效果?

javascript - jQuery 插件覆盖参数

c# - 这是不推荐使用的编码方式吗? ASP.Net MVC

c# - WPF Datagrid - 强制单行行

windows - x :Uid generator for Windows 8 projects

c# - 将控件的可见性绑定(bind)到 IEnumerable 的 'Count'

javascript - 将愿望 list 按钮替换为“添加到购物车”