react-native-webview-android icon indicating copy to clipboard operation
react-native-webview-android copied to clipboard

How do I receive a message sent by a webview in an html file when I load html?

Open xpbtmusic opened this issue 6 years ago • 1 comments

How do I receive a message sent by a webview in an html file when I load html? The official webview is using document.addEventListener('message', function (e) {}

xpbtmusic avatar Aug 01 '18 10:08 xpbtmusic

Hi @xpbtmusic

If you need just sent a data when your html file load, you could use the prop injectedJavaScript instead.

Something like:

return (
  <WebViewAndroid
    ref="webViewAndroidSample"
    javaScriptEnabled={true}
    injectedJavaScript={`window.myMessageToRecover = 'Hello from React Native';`}
    url={"https://yourwebsite.com"}
    style={{ flex: 1 }} />
);

And then, in your HTML you could recover the variable window.myMessageToRecover in on page ready or in your page load events.

lucasferreira avatar Aug 01 '18 13:08 lucasferreira