【发布时间】:2020-06-05 03:29:33
【问题描述】:
我正在尝试在加载到 iOS 建议的 WebView 中执行 javascript。我试图让一个非常简单的示例工作,但它始终抛出未定义或 TypeError。
代码如下:
import React, { Component } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { WebView } from 'react-native-webview';
export default class App extends Component {
constructor(props){
super(props)
this.onPressButton = this.onPressButton.bind(this)
}
onPressButton(){
this.webview.injectJavascript(`alert('hello')`)
}
render() {
return (
<View style={{ height: '100%' }}>
<WebView
ref={ref => (this.webview = ref)}
javaScriptEnabled={true}
source={{ uri: 'https://google.com' }}
/>
<Button onPress={this.onPressButton} style={{ height: '10%' }} title="Test Javascript" />
</View>
);
}
}
请不要推荐使用injectedJavascript,因为那不是我想要的功能。
也希望有任何其他方法。
【问题讨论】:
标签: javascript ios reactjs react-native webview