question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

injectedJavaScript no working

See original GitHub issue

Hello,

Bug description: I have UPGRADE my old app, for accept by Apple i need remove UIWebview, so i have update react-native-webview 6.0.0 to 10.3.2 but now injectedJavaScript no working…

My old code :

const disableZoom = "const meta = document.createElement('meta'); meta.setAttribute('content', 'initial-scale=1.0, maximum-scale=1.0'); meta.setAttribute('name', 'viewport'); document.getElementsByTagName('head')[0].appendChild(meta);";

<WebView
   style={stylesLive.webview}
   source={{uri: 'https://www.twitch.tv/embed/hebo/chat?parent=tiboy.fr'}}
   injectedJavaScript={disableZoom}
   scalesPageToFit={false}
   useWebKit={true}
/>

I have try :

const jsCode = "document.querySelector('.HeaderHero').style.backgroundColor = 'purple';";

<WebView
 ref={ref => {
  this.webview = ref;
 }}
 source={{ uri: 'https://reactnative.dev' }}
 javaScriptEnabledAndroid={true}
 injectedJavaScript={jsCode}
/>

How to pass code now? I would like to deactivate the zoom of the webview, especially when I select a field it zooms me all without zooming out after … two days that I test, search all the possible posts on the net and test other codes …

Environment:

  • OS: MAC OS X
  • OS version: 10.15.5
  • react-native version: 0.62.2
  • react-native-webview version: 10.3.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
lrussocommented, Aug 18, 2020

@Linrasis, the documentation in the react-native-webview repository about how the injectedJavaScript property works is outdated. Now you need to set an onMessage event (an empty one it’s ok) in order to the code within the injectedJavaScript property to be executed. Below you can see an example of this. Hope it may help you.

    const runFirst =
        "let meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=600'); document.getElementsByTagName('head')[0].appendChild(meta);"

    return (
        <WebView
            source={{ uri }}
            originWhitelist={['*']}
            startInLoadingState={true}
            onError={() => setError(true)}
            onMessage={(event) => {}}
            injectedJavaScript={runFirst}
        />
    )
0reactions
vishwa1937commented, Jul 20, 2021

@Linrasis, the documentation in the react-native-webview repository about how the injectedJavaScript property works is outdated. Now you need to set an onMessage event (an empty one it’s ok) in order to the code within the injectedJavaScript property to be executed. Below you can see an example of this. Hope it may help you.

    const runFirst =
        "let meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=600'); document.getElementsByTagName('head')[0].appendChild(meta);"

    return (
        <WebView
            source={{ uri }}
            originWhitelist={['*']}
            startInLoadingState={true}
            onError={() => setError(true)}
            onMessage={(event) => {}}
            injectedJavaScript={runFirst}
        />
    )

Thank You @lrusso. This worked for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

injectedJavaScript is not working in IOS · Issue #1779 - GitHub
I am using "injectedJavaScript" in webview, this property is working fine in android but not working in ios. Code import React, { Component...
Read more >
injectedJavaScript is not working in Webview of react native
Well, you have more than one issue here. First one is your web view needs to be wrapped in a containing View component...
Read more >
why react native webview injectedJavascript doesn't work on ...
Coding example for the question why react native webview injectedJavascript doesn't work on IOS?-React Native.
Read more >
Need help with react-native Webview, injecting react ... - Reddit
The string interpolation doesn't work. What should I do if I'm trying to pass data from react native into the javascript string that's ......
Read more >
React Native WebView: A complete guide - LogRocket Blog
The JavaScript code that is provided to the injectedJavaScript prop is only going to be ... Without this prop, the scripts will not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found