Variables and functions from injectedJavaScript not available on Android
See original GitHub issueBug description:
On Android, variables and functions declared in injectedJavaScript
are not available in code run by injectJavaScript
, whereas on iOS they are. Please note that I’m using source={{ html: ... }}
, not loading an external URI. I believe the behaviour on iOS is correct, that on Android is not.
To Reproduce: I created a repo that demonstrates this issue. I was careful to create a descriptive git history, so I hope it is pretty self-explanatory: https://github.com/svdo/VarIssue. This repo contains a complete react native project that you should be able to run yourself (make sure Android emulator is running):
yarn
yarn react-native start --reset-cache
yarn react-native run-ios
yarn react-native run-android
Environment:
- OS: Android 9 and iOS 11
- react-native-webview version: 5.8.1
react-native info
:React Native Environment Info: System: OS: macOS 10.14.4 CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz Memory: 18.87 MB / 16.00 GB Shell: 5.3 - /bin/zsh Binaries: Node: 11.14.0 - /var/folders/0d/5_t6qk7s06qf94jnypzwh6yh0000gp/T/yarn--1557086445011-0.06815149805556375/node Yarn: 1.15.2 - /var/folders/0d/5_t6qk7s06qf94jnypzwh6yh0000gp/T/yarn--1557086445011-0.06815149805556375/yarn npm: 6.9.0 - ~/.homebrew/bin/npm SDKs: iOS SDK: Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2 IDEs: Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild npmPackages: react: 16.8.3 => 16.8.3 react-native: 0.59.5 => 0.59.5
Note
- Using
injectJavaScript
to declare the variables and function, instead ofinjectedJavaScript
, will cause everything to work as expected. This is demonstrated in commit https://github.com/svdo/VarIssue/commit/72f3fa0ed729a5c226a615144380f4c5a7a92997. - This may or may not be related to issue #445.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:6
Top Results From Across the Web
React Native Webview - injectJavascript not working
Hi, how do i pass a value from my react native app to my webview. eg. i have a session id created when...
Read more >Need help with react-native Webview, injecting ...
I figured out the answer. It's really nuanced. Essentially you have to create a global window variable before the injectedJavascript() method is ...
Read more >Inject JS into Android's WebView. ...
Exposing JS function calls out of WebView. There is a way to bridge JavaScript methods called in WebView and your Java/Kotlin code.
Read more >@sishuguojixuefu/react-native-echarts-wrapper NPM | npm.io
On the other hand it is not easy to make a two way data communication ... Variables and functions from injectedJavaScript not available...
Read more >WebView
... if not specified. On Android, the only supported methods are GET and POST. ... Function that returns a view to show if...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I solved this by using the onLoad prop. I load the following function into onLoad:
I red that you should care about the linebreaks.
Using this method I can access variables when calling the injectJavaScript method.
This is still a problem on version: 9.1.4. I could not try 9.2.0 because of another known build error. Once the fix for 9.2.0 is released I try it as well. But for the moment the workaround that @svdo posted above worked for me!