如何获取在HTML页面的WebView中发送的变量的值?

时间:2019-07-16 21:13:27

标签: react-native

我正在开发一个App,该App需要将变量发送到将在WebView中显示的html页面。这是React Native Application代码的基本示例。

export default class App extends Component {


  render()
  {

   let variableCadena="React Native";




    return(

      <Container>
        <WebView
        style={{flex: 1}}
        originWhitelist={['*']}
        source={{uri:'file:///android_asset/PaginaEjemplo.html'}}
         style={{ marginTop: 20 }}
        javaScriptEnabled={true}
        domStorageEnabled={true}
        allowFileAccess={true}
        allowUniversalAccessFromFileURLs={true}
        injectedJavaScript={variableCadena} 
        >

      </WebView>      
      </Container>
    );
  }
};

HTML可以像下面的一样简单。

    <html>
    <head>
    <title>Ejemplo de inyeccion desde React Native</title>
    <script type="text/javascript">
        var variable = variableCadena;
    </script>
    </head>

    <body>
    <script type="text/javascript">

    document.body.innerHTML = "<h1>La variable es:"
    +variable+ "</h1>"

   </script>
   </body>
   </html>

预期结果是网页在h1标签中显示了应用程序中定义的React Native文本。谢谢大家的建议。

1 个答案:

答案 0 :(得分:0)

首先,我希望您使用https://github.com/react-native-community/react-native-webview而不是from react-native,因为它现在已被弃用。

您可以使用injectJavascript(...)方法。像这样...

import React, { Component } from 'react';
import { View } from 'react-native';
import { WebView } from 'react-native-webview';

export default class App extends Component {
  render() {
    const run = `
      document.body.style.backgroundColor = 'blue';
      true;
    `;

    setTimeout(() => {
      this.webref.injectJavaScript(run);
    }, 3000);

    return (
      <View style={{ flex: 1 }}>
        <WebView
          ref={r => (this.webref = r)}
          source={{
            uri: 'https://stackoverflow.com/questions/57065527',
          }}
        />
      </View>
    );
  }
}

这会给你这样的东西:

现在解决这个问题!查看小吃:https://snack.expo.io/@abranhe/stackoverflow-57065527

  

更新作者使用变量请求演示

App.js

import React, { Component } from 'react';
import { View } from 'react-native';
import { WebView } from 'react-native-webview';

export default class App extends Component {
    injectjs() {
        let name = 'Abraham';

        setTimeout(() => {});
        return `document.getElementById('inject').innerHTML = '<h1>The  name is <b>${name}</b></h1>'`;
    }
    render() {
        return (
            <View style={{ flex: 1 }}>
                <WebView
                    ref={(r) => (this.webref = r)}
                    injectedJavaScript={this.injectjs()}
                    source={require('./demo.html')}
                />
            </View>
        );
    }
}

demo.html

<html>
  <head>
    <title>Ejemplo de inyeccion desde React Native</title>
    <style>
      body {
        background-color: aquamarine;
        display: -webkit-flex;
        -webkit-justify-content: center;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
      }
    </style>
  </head>
  <body>
    <img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=a010291124bf" />
    <div id="inject">
    </body>
</html>

结果

enter image description here