Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why react native webview injectedJavascript doesn't work on IOS?

Tags:

react-native

I try to call a function from injectedJavascript of webview , in android it works fine , but on IOS it didn't work , could you help me on that , this is my webview :

<View style={container}>
        <WebView
          javaScriptEnabled={true}
          domStorageEnabled={true}
          injectedJavascript={this.jsCode.bind(this)}
          onNavigationStateChange={this.onNavigationStateChange}
          source={webapp}
          ref="WEBVIEW_REF"
          style={webView}
          decelerationRate="fast"
        />

      </View >

my function is :

jsCode() {
  let linn ='document.querySelector(".spl-bg2").style.color="blue";';
  return linn;
 }
like image 896
salmanaacvf Avatar asked Mar 15 '26 09:03

salmanaacvf


1 Answers

injectedJavaScript is supposed to be a string that gets evaluated as javascript when the WebView loads so in your case it should work like this

let linn ='document.querySelector(".spl-bg2").style.color="blue";';
return(
  <View style={container}>
    <WebView
      javaScriptEnabled={true}
      domStorageEnabled={true}
      injectedJavaScript ={linn}
      onNavigationStateChange={this.onNavigationStateChange}
      source={webapp}
      ref="WEBVIEW_REF"
      style={webView}
      decelerationRate="fast"
    />
  </View>)

Update

This is a working example of the above, that runs on a website and changes the colour of the first paragraph it finds:

https://snack.expo.io/rJHiavAwX

Have a look at it and maybe you will find your issue. Note that the prop name is injectedJavaScript and not injectedJavascript as you have it in your example.

like image 133
needsleep Avatar answered Mar 16 '26 22:03

needsleep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!