react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[WebView] WebView on android can`t execute javascript

Open magicismight opened this issue 10 years ago • 29 comments

              <WebView
                style={[styles.webview, {
                    height: this.state.height || 400
                }]}
                javaScriptEnabled={true}
                bounces={false}
                onNavigationStateChange={this._onNavigationStateChange}
                html={this._getHTML(content)}
            />

javascript inside WebView page do not execute. On iOS it works fine.

magicismight avatar Jan 06 '16 08:01 magicismight

Hey magicismight, thanks for reporting this issue!

React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.

  • If you don't know how to do something or something is not working as you expect but not sure it's a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
  • If this is a feature request or a bug that you would like to be fixed, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
  • We welcome clear issues and PRs that are ready for in-depth discussion. Please provide screenshots where appropriate and always mention the version of React Native you're using. Thank you for your contributions!

facebook-github-bot avatar Jan 06 '16 08:01 facebook-github-bot

You need to enable JavaScript via a a prop - http://facebook.github.io/react-native/docs/webview.html#javascriptenabled

satya164 avatar Jan 06 '16 10:01 satya164

@satya164 I have tried that. Didnt work. Tried to use injectedJavaScript didnt work either

magicismight avatar Jan 06 '16 10:01 magicismight

I tried to use react-native-webview-android the Javscript inside page didnt execute. But javascript through injectedJavaScript` worked

magicismight avatar Jan 06 '16 10:01 magicismight

Can you give a minimal reproducable example of the code that's not working?

cc @mkonicek

satya164 avatar Jan 06 '16 10:01 satya164

We don't provide supports third party plugins. Have you tried the one included in React Native?

satya164 avatar Jan 06 '16 10:01 satya164

@satya164 yes. after I tried everything in React Native.it didn`t work out,then I tried the third party plugin.

magicismight avatar Jan 06 '16 10:01 magicismight

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  WebView
} = React;

let html =  `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <style>
    html,
    body{
        overflow: hidden;
    }
    *{
        padding: 0;
        margin: 0;
        font-size: 14px !important;
        color: #666 !important;
        max-width: 100% !important;
    }
    </style>
</head>
<body>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
</body>
<script>
(function () {

    document.write('nothing');
}());
</script>
</html>
`;

var marry = React.createClass({

  render: function() {
    return (
      <View style={styles.container}>
        <WebView
                style={[{
                    height: 400,
                    width: 320
                }]}
                javaScriptEnabled={true}
                bounces={false}
                onNavigationStateChange={this._onNavigationStateChange}
                html={html}
            />
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('marry', () => marry);


qq 20160106182426

magicismight avatar Jan 06 '16 10:01 magicismight

same code. different results.

magicismight avatar Jan 06 '16 10:01 magicismight

closed The attribute should be javaScriptEnabledAndroid instead of javaScriptEnabled as document said.

magicismight avatar Jan 07 '16 07:01 magicismight

javaScriptEnabledAndroid is only used on 0.17.0 0.18.0 javaScriptEnabled works fine

magicismight avatar Jan 07 '16 08:01 magicismight

Is there a resolution for this problem? As of 0.20 I still can't get Javascript to work on Android.

bomalley avatar Feb 19 '16 00:02 bomalley

Also can't get it to work... Here's my render:

render() {
    let html = '<div id="myContent">This is my name</div>';
    let jsCode = 'document.querySelector("#myContent").style.backgroundColor = "red"';

    return (
      <View>
        <WebView
          source={{html: html}}
          injectedJavascript={jsCode}
          javaScriptEnabled={true}
          style={this.props.style}
        />
      </View>
    );
  }

And the result:

screen shot 2016-02-28 at 01 50 28

The div background should be red.

LightningK0ala avatar Feb 28 '16 01:02 LightningK0ala

It seems to work if I include the javascript as a script in html like this:

render() {
  let html = `
    <div id="myContent">This is my name</div>
    <script>
      document.querySelector("#myContent").style.backgroundColor = "red"\
    </script>
  `;

  return (
    <View>
      <WebView
        source={{html: html}}
        javaScriptEnabled={true}
        style={{ width: 100, height: 100}}
      />
    </View>
  );
}

LightningK0ala avatar Mar 01 '16 11:03 LightningK0ala

Nothing is working is there is any other method to do it

uc-des avatar Jul 09 '16 11:07 uc-des

Worked for me by adding baseUrl like this:

<WebView javaScriptEnabled={true} source={{html: myHtml, baseUrl: "/"}} />

nopopon avatar Jul 26 '16 14:07 nopopon

Set true both javaScriptEnabled and domStorageEnabled

javaScriptEnabled={true}
domStorageEnabled={true}

iineva avatar Nov 30 '16 12:11 iineva

on android I got it finally to work after I've put semicolon ";" at the end of every command, for example;

this works:

let jsCode = `
      document.body.style.backgroundColor = \'#6DAF45\';

      var menuButton = document.getElementById("responsive-menu-button");
      menuButton.parentNode.removeChild(menuButton);

      var header = document.getElementById("header");
      header.style.visibility = \'hidden\';
      header.style.height = 0;

      var innerBannerArea = document.getElementById("inner-banner-area");
      innerBannerArea.style.height = 1;
      innerBannerArea.style.opacity = 0.0;

      var footer = document.getElementById("footer");
      footer.style.visibility = \'hidden\';
    `

and this breaks the whole javascript, check line 2 in "jsCode" in the variable:

let jsCode = `
      document.body.style.backgroundColor = \'#6DAF45\';

      var menuButton = document.getElementById("responsive-menu-button");
      menuButton.parentNode.removeChild(menuButton)

      var header = document.getElementById("header");
      header.style.visibility = \'hidden\';
      header.style.height = 0;

      var innerBannerArea = document.getElementById("inner-banner-area");
      innerBannerArea.style.height = 1;
      innerBannerArea.style.opacity = 0.0;

      var footer = document.getElementById("footer");
      footer.style.visibility = \'hidden\';
    `

menuButton.parentNode.removeChild(menuButton) <- is missing semicolon

Goran1708 avatar Mar 07 '17 09:03 Goran1708

The issue seems to depend on the android SDK version. using 23 fails and it works using version 24 and 25 . this might require more testing on real device like samsung to see if it works on 23 on some real devices.

jpoutrin avatar Mar 10 '17 18:03 jpoutrin

@iineva You saved me. I searched whole day. My opencart checkout page is not working in android webview. But by adding setDomStorageEnabled(true) it's worked like charm. Thank you so much.

atulamuwa avatar May 30 '17 12:05 atulamuwa