【问题标题】:React native: WebView Auto heigh bugged on Android - javascript injection is not executing反应原生:Android 上的 WebView Auto heigh 错误 - javascript 注入未执行
【发布时间】:2016-05-15 01:05:36
【问题描述】:

我真的需要在 Android 和 iOS 上制作一个 WebView,它将为静态 html 设置自动高度。我进行了研究,发现 WebView 没有该功能。因此,我尝试实现一些本机库,它们再次适用于 iOS,但不适用于 Android。此外,我发现了一些主题如何将 javascript 注入 html 并获取 height 的值并在之后为 WebView 动态设置它。但是,这些脚本可以在 iOS 上运行,但不能在 Android 上运行。

我尝试了这些解决方案,但它们不适用于 Android,即使我为 Android WebView 启用了 javascript。我个人认为,即使我这样配置 WebView,注入的 javascript 也不会在 Android 上执行:

    <WebView                            
        html={html}
        javaScriptEnabled={true}
        injectedJavaScript="document.body.scrollHeight;"
        scrollEnabled={false}
        onNavigationStateChange={this._updateWebViewHeight}
        automaticallyAdjustContentInsets={true}
        style={{height: this.state.webViewHeight}}
    />

这是一个截图,我从onNavigationStateChange打印出事件对象,一个来自Android,另一个来自iOS,它们没有相同的字段:(请帮助。谢谢

Screenshot for Android emulator and iOS emulator with printed event object

【问题讨论】:

    标签: android ios webview react-native javascript-injection


    【解决方案1】:
    1. 您可以使用react-native-html-webview。它对我来说效果很好并且易于使用。 var HTMLWebView = require('react-native-html-webview'); <HTMLWebView html = {htmlContent} makeSafe = {true} autoHeight = {true}/>
    2. 看看这个要点:WebViewResizing.js

    【讨论】:

    • 谢谢你的朋友,但它只适用于iOS,问题出在Android
    • option2 在 android 上有效吗?如果您找到了解决方案,请分享。
    猜你喜欢
    • 2017-07-19
    • 2018-02-08
    • 2019-11-10
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多