New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
injectedJavaScript doesnt seem to work on IOS #1291
Comments
According to the guide, you now must pass a function as the |
@tdcook that seems to have fixed the IOS issue but now the main issue is Android & IOS can only access whats injected at seperate times. window.onload works for IOS to try and get the values but when put into android it only returns undefined. "injectedJavaScriptBeforeContentLoad" seems like a requirement almost for both platforms, is there a way to simulate this since its not built in? |
This is not a regression; it’s long been the case that you’ve needed to pass onMessage in order for JS injection to work (and I’ve never liked it). It’s also actually documented, but admittedly hard to find. Here’s my line-by-line explanation of how to fix it, in case anyone feels like implementing a PR: #1260 (comment) |
@shirakaba thanks, Looking forward to the merger and normal use of this module |
Did not see this issue until just now, but this should be resolved in: #1286 which is now in the latest release! |
Someone could provide a minor gist or working example to simply change the background color? |
@swallville that's already documented here in the "Communicating between JS and Native" guide, which is linked off to by the API docs for |
Could we close this issue now that I've created the more specific issue #1311 (which addresses the root cause) to track it instead? |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
I am already using 'onMessage'. Still it is not working to inject javascript. |
@PrantikMondal - I had the same issue, the JS didnt inject itself into the webview unless i injected it with the help of ref export default class App extends Component {
} |
I was able to get this to work with functional components Without the ref, for some reason injectedJavaScript will not run. With the ref set as
|
Thanks @vikbos, good solution for this issue. Here is my code. import React, { useRef } from 'react'
import { WebView } from 'react-native-webview'
const INJECTED_JAVASCRIPT = `(function() {
// your code
})();`
const YourScreen = ({ navigation }) => {
const webview = useRef(null)
const _handleLoadEnd = () => {
webview.current.injectJavaScript(INJECTED_JAVASCRIPT)
}
return (
<WebView
javaScriptCanOpenWindowsAutomatically
onLoadEnd={_handleLoadEnd}
ref={webview}
source={{ uri }}/>
)
} Don't add property |
thank you very much this code is running fine in my app |
Bug description:
Setup the WebView and try to use the "injectedJavaScript" prop. It works as expected on Android but when on IOS it does nothing.
To Reproduce:
Expected behavior:
Expected to render the github page for react-native-webview with a red background on both platforms.
Screenshots/Videos:
Android:
IOS:
Environment:
The text was updated successfully, but these errors were encountered: