JavaScript and jQuery is the base of almost every web application(Websites) present on internet because of its flexible environment. But infrequently hacker or bad peoples needs to inject some bad code in user’s device using JavaScript, So here this features helps the android and iOS application user to stop executing JavaScript and jQuery on their webpage loaded inside WebView in device. So in this tutorial we would going to make a react native app with Enable Disable JavaScript Code functionality and Stop executing JavaScript + jQuery code on their WebView using WebView’s javaScriptEnabled={Boolean Value} prop in Android iOS application.
Contents in this project Enable Disable JavaScript Code in WebView React Native Example :
1. Import StyleSheet, Platform and WebView component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, Platform, WebView} from ‘react-native’;
|
2. Create WebView component inside render’s return block with javaScriptEnabled={Boolean Value} Prop.
- If we set javaScriptEnabled={true} then it will Enable the JavaScript on WebView.
- If we set javaScriptEnabled={false} then it will Disable the JavaScript on WebView.
1
2
3
4
5
6
7
8
9
10
11
12
|
render() {
return (
<WebView
style={styles.WebViewStyle}
source={{uri: ‘https://Google.com’}}
javaScriptEnabled={false}
domStorageEnabled={true} />
);
}
|
3. Create Custom Style :
1
2
3
4
5
6
7
8
9
10
11
|
const styles = StyleSheet.create(
{
WebViewStyle:
{
justifyContent: ‘center’,
alignItems: ‘center’,
flex:1,
marginTop: (Platform.OS) === ‘ios’ ? 20 : 0
}
});
|
4. Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import React, { Component } from ‘react’;
import { StyleSheet, Platform, WebView} from ‘react-native’;
export default class MainActivity extends Component {
render() {
return (
<WebView
style={styles.WebViewStyle}
source={{uri: ‘https://Google.com’}}
javaScriptEnabled={false}
domStorageEnabled={true} />
);
}
}
const styles = StyleSheet.create(
{
WebViewStyle:
{
justifyContent: ‘center’,
alignItems: ‘center’,
flex:1,
marginTop: (Platform.OS) === ‘ios’ ? 20 : 0
}
});
|
Screenshot in iOS device :
Screenshot in Android App: