react-native: Webview is not executing my JavaScript from the web page on Android?

Description

When I open WebView on Android emulator or real device it does not execute JavaScript. I have run simple Android App in Android Studio and it worked there.

Reproduction Steps

Just create a new app and add this WebView to it

Sample Code

<WebView
                style={[styles.flex]} //styles
                javaScriptEnabled={true} //enabling JavaScript
                source={{uri: 'http://partners.zeew.eu/'}} //Url
            />

Solution

Can you please check it and say what is wrong?

Additional Information

  • React Native version: 0.45.1
  • Platform: Android ( not sure about iOS )
  • Development Operating System: Windows
  • Build tools: Android Studio: Nexus 5x edited_api_25 Android 6.0 Google APIs, Nexus 5 API level 26, Android 8.0.0

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 7
  • Comments: 48 (5 by maintainers)

Most upvoted comments

The community does not seem to be really strong and supportive.

@jayporta have u tried with baseUrl=" " baseUrl with empty string.

Hey I’m also having a similar problem. Some of the javascript in my Android webview will fail to execute, but there seems to be no way to get debug information or error messages. Does anyone know how to read the console on the embedded webview? I have resorted to doing stuff like this:

<div id="hello"></div>
<script>
document.getElementById('hello').textContent = "Hello World....";
</script>

Which obviously does not always work because sometimes javascript fails to execute in the webview and I cannot see the console output.

Javascript execution in webview is not working for android API version 21 and below. Anyone found any solution?

@pankaj-ag Thank you!

Why does an emulator WebView not work when same emulator’s native browser does work?

Try running on a real Android device.

  • On an x86 Android emulator, I can NOT get a WebView to run an external URL that returns a webpack ( Angular ) bundle ( Other web pages work fine )
  • The same URL works fine if copied to the x86 Android emulator’s native web browser.
  • The same URL works fine running on a WebView on a real Android Device.
  • Other URLs with embedded JS run fine in WebView on emulator.

I am not sure if anything more than javaScriptEnabled is needed.

      render() {
        return (
          <WebView
            source={{ uri: this.props.uri }}
            style={{ marginTop: 0 }}
            scalesPageToFit
            javaScriptEnabled
            domStorageEnabled
            startInLoadingState
            mixedContentMode="always"
          />
        );
      }

somehow its not working in android emulator but its working fine in my device.

@ggsrivas thx! I have tried and find this problem, too. I used let… hah

can anyone tell me how to include <script> tag in webview?

For me the page loaded in the webview has no problem using JavaScript, but the injected JavaScript doesn’t get executed without javaScriptEnabled set to true