Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript not work in webview #8996

Closed
imlooke opened this issue Jul 24, 2016 · 22 comments
Closed

JavaScript not work in webview #8996

imlooke opened this issue Jul 24, 2016 · 22 comments
Assignees
Labels
Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.

Comments

@imlooke
Copy link

imlooke commented Jul 24, 2016

When I use the webview in my project,JavaScript not work in the HTML file,Here my code:
``
`

    import React, { Component } from 'react';
    import {
      StyleSheet,
      Text,
      View,
      WebView,
    } from 'react-native';

    export default class NoteEditor extends Component {
      render() {
        return (
          <WebView 
            style={styles.container} 
            source={require('./editor.html')}
            injectedJavaScript={'function myfunc() { alert(123) };myfunc();'}
            javaScriptEnabled={true}
            startInLoadingState={true}
          />

        );
      }
    };

    const styles = StyleSheet.create({
      container: {
        flex: 1,
      },
    });

`

-I use RN 0.29.2
-in RN 0.27 is ok
what should I do? thanks

@Kureev
Copy link
Contributor

Kureev commented Jul 25, 2016

iOS / Android?

@ChinaGoodProgrammer
Copy link

me too!

@Kureev
Copy link
Contributor

Kureev commented Jul 25, 2016

Just tried on the fresh project:

![image](https://cloud.githubusercontent.com/assets/2273613/17095219/aa43067e-5253-11e6-9ff6-cb2455686765.png)

Corresponding code:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  WebView,
  View
} from 'react-native';

class example extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
                <WebView
          automaticallyAdjustContentInsets={false}
          style={{height: 350, width: 300}}
          source={require('./test.html')}
          javaScriptEnabled={true}
          domStorageEnabled={true}
          decelerationRate="normal"
          startInLoadingState={true}
          scalesPageToFit={true}
        />
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
}

const 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('example', () => example);

@Kureev
Copy link
Contributor

Kureev commented Jul 25, 2016

I think you have a problem with styling. Try fixed dimensions and/or adjust your layout. Let me know if it helps.

@Kureev Kureev self-assigned this Jul 25, 2016
@Kureev
Copy link
Contributor

Kureev commented Jul 25, 2016

Your example with alert also works:

![image](https://cloud.githubusercontent.com/assets/2273613/17095416/9b72310a-5254-11e6-846f-f143612330c6.png)

@ChinaGoodProgrammer
Copy link

Offline package does not work!

@ChinaGoodProgrammer
Copy link

in iOS。

@ChinaGoodProgrammer
Copy link

The injection of js code doesn't work in the release environment。

@ChinaGoodProgrammer
Copy link

@Kureev

@ChinaGoodProgrammer
Copy link

@Kureev
Please help me thank you!

@imlooke
Copy link
Author

imlooke commented Jul 25, 2016

@Kureev,Android,
I test your code as same, but not work all the time
And I try to update RN to 0.30,not work, too
I even test this in three phones ,dose not work....
android6.0,5.1,4.4,
but in RN 0.27 is work..

@Kureev
Copy link
Contributor

Kureev commented Jul 25, 2016

I think the problem is in the release mode. Probably, something is broken in the packager.

@imlooke
Copy link
Author

imlooke commented Jul 25, 2016

what should I do for this time?back to RN 0.27?,or init a new project?
thanks for your help

@imlooke
Copy link
Author

imlooke commented Jul 25, 2016

@Kureev,it must be.now I back to 0.27 is not work,too.
**something is broken in the packager.**what should I do to solve this?

@grabbou
Copy link
Contributor

grabbou commented Jul 28, 2016

@Kureev shouldn't this issue be converted to StackOverflow?

@Kureev
Copy link
Contributor

Kureev commented Jul 28, 2016

@grabbou not sure. Probably, this issue can be reworded to "WebView doesn't work in Release mode".

@namse
Copy link
Contributor

namse commented Jul 31, 2016

I installed 0.30 with cli command react-native init AwesomeProject, and webview works pretty well even on release offline bundle mode.

@Kureev
Copy link
Contributor

Kureev commented Jul 31, 2016

Then I'm about to close this one as far as its fixed in 0.30.

@Kureev
Copy link
Contributor

Kureev commented Jul 31, 2016

@facebook-github-bot close

@ghost
Copy link

ghost commented Jul 31, 2016

@Kureev tells me to close this issue. If you think it should still be opened let us know why.

@ghost ghost closed this as completed Jul 31, 2016
@ghost ghost added the Ran Commands One of our bots successfully processed a command. label Jul 31, 2016
@tapradhan
Copy link

Javascript in webview is not working in ios after release.but works in debugging mode

@arash-hacker
Copy link

#16133
hey guys check my answer
it work with RN >=0.54

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

8 participants