A
A
Andrey Okhotnikov2020-02-25 11:29:37
React Native
Andrey Okhotnikov, 2020-02-25 11:29:37

How to send message in iframe in webview?

I have a webview component

<WebView
          ref={webView => (this.webView = webView)}
          originWhitelist={['*']}
          source={{
            html: `<iFrame id="payture-iframe" name="paytureIframe" width="100%" height="450px" src=${this.props.url} frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iFrame>`,
          }}
          onMessage={this.onMessageHandler}
          style={{ flex: 1, width: '100%', height: '100%' }}
        />


There is an object with the postMessage method, I send a message through it, but the frame does not see it

I send a message
this.webView.postMessage(JSON.stringify({ type: 'sendOrder' }), '*');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrovich, 2020-02-25
@RomReed

There is 1 request to pass data to webview. To do this, you need to call the function like this

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>WebView1</title>
<meta forua="true" http-equiv="Cache-Control" content="max-age=0"/>
</head>
<script type="text/javascript">
function testEcho(test){
  alert(test);
}
</script>
</body>
</html>

return this.webview&&this.webview.injectJavaScript("testEcho("+JSON.stringify({...result,token:this.state.accessToken})+");")

the testEcho function must be in the iFrame

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question