0

I get a challenge when apply webview. I want to inject Javascript function to my webview, but it only work in iOS not in Android.

Look to my code :

Webview :

<WebView
                            source={{uri:"http://mywebview.com/webview.php"}}
                            injectedJavaScript={jsCode}
                            mixedContentMode={'compatibility'}
                            javaScriptEnabledAndroid={true}
                            style={{height: 300}} />

JSCode to Inject :

let jsCode = `function doPopUp() {
                        document.querySelector('#myBody').style.backgroundColor = 'red';
                        alert('hello world from webview');
                        }`;

In iOS it works fine, but not in Android. If i not inject the JS (i put it directly in php file) and then open in Android Browser it works fine. Additional information for you is, if i'm not put syntax inside a js function it works fine. Why? and how to fix it?

5
  • set domStorageEnabled={true} in WebView. hope it works Commented Nov 15, 2018 at 4:39
  • unfortunately not working for me. Commented Nov 15, 2018 at 6:07
  • where do you call doPopUp() function in your http://mywebview.com/webview.php file?? Do you have any HTML component with id myBody?? Commented Nov 15, 2018 at 6:51
  • I call it inside button. then function not working only if i inject the script via React Native to Webview. If i put the function inside Webview directly it's working. Commented Nov 16, 2018 at 0:34
  • remove single quotes while declaring let jsCode Commented Nov 16, 2018 at 3:45

2 Answers 2

2

I was able to find a solution to this problem after a few hours of trying. Something like this will work.

Without the ref, for some reason injectedJavaScript will not run. With the ref set, webViewRef.current.injectJavaScript(runFirstScript); , injectedJavaScript will run as well.

This was only an issue in IOS, not android.

I've posted this solution to the github issues for react-native-webview as well. https://github.com/react-native-webview/react-native-webview/issues/2259

 ...
 
  const webViewRef = useRef();

 const runFirstScript = `
      document.body.style.backgroundColor = 'red';
      setTimeout(function() { window.alert('hi') }, 2000);
      true; // note: this is required, or you'll sometimes get silent failures
    `;
    
    const runSecondScript = `
      setTimeout(function() { window.alert('hello') }, 2000);
      true; 
    `;

  if (webViewRef.current != null) {
    webViewRef.current.injectJavaScript(runSecondScript);
  }
  return (
    <>
      <WebView
        ref={ref => (webViewRef.current = ref)}
        cacheEnabled={true}
        injectedJavaScript={runFirstScript} // now injectedJavaScript will trigger along with the ref.
        javaScriptEnabled
        mixedContentMode={'compatibility'}
        onMessage={event => {
          //    alert(event.nativeEvent.data);
        }}
        originWhitelist={['*']}
        scalesPageToFit
        source={{uri: 'https://example.com'}}
        startInLoadingState={true}
        userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
      />
    </>
  );```
Sign up to request clarification or add additional context in comments.

Comments

1

For android, while adding the javascript function we need to add it as part of DOM. For that, replace function with document in jsCode.

let jsCode = `docuement.doPopUp() {
                        document.querySelector('#myBody').style.backgroundColor = 'red';
                        alert('hello world from webview');
                        }`;

Comments

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.