3

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;
 }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for ur help , but unfortunately it didn't work , even with adding a single quote to linn ..
I've updated my answer by adding a simple snack to demonstrate the functionality . If you're still having problems maybe set up a snack yourself and I will have a look. Good luck!
it wroks only if you put a direct link (ex : www.mysite.com) , in my case in source variable I am calling a local file (index.html) that contains links to visit
Hm...It works with local html files too... Which makes me think that there is something wrong with the html file you're importing, maybe a javascript error that prevents your code from running. Is there any way to share the imported html file or a part of it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.