admin管理员组文章数量:1287608
I'm trying to inject JS code to alert the user before the content of the React Native Webview loads, however, for me it just goes to the activity indicator and loads the Webview without injecting any Javascript. How do I fix this? Code below:
import React from 'react';
import { View, ActivityIndicator } from 'react-native';
import { WebView } from 'react-native-webview';
import styles from '../../styles';
export default function Links() {
function LoadingIndicatorView() {
return (
<View style={styles.hubLoading}>
<ActivityIndicator color='#009b88' size='large' />
</View>
)
}
const runFirst = `
setTimeout(function() { window.alert('hi') }, 2000);
true; // note: this is required, or you'll sometimes get silent failures
`
return <WebView source={{ uri: / }} renderLoading={LoadingIndicatorView} startInLoadingState={true} javaScriptEnabled={true} injectedJavaScript={runFirst} />;
}
Btw, I am running the app on my iOS device if that helps you to answer.
I'm trying to inject JS code to alert the user before the content of the React Native Webview loads, however, for me it just goes to the activity indicator and loads the Webview without injecting any Javascript. How do I fix this? Code below:
import React from 'react';
import { View, ActivityIndicator } from 'react-native';
import { WebView } from 'react-native-webview';
import styles from '../../styles';
export default function Links() {
function LoadingIndicatorView() {
return (
<View style={styles.hubLoading}>
<ActivityIndicator color='#009b88' size='large' />
</View>
)
}
const runFirst = `
setTimeout(function() { window.alert('hi') }, 2000);
true; // note: this is required, or you'll sometimes get silent failures
`
return <WebView source={{ uri: https://www.google./ }} renderLoading={LoadingIndicatorView} startInLoadingState={true} javaScriptEnabled={true} injectedJavaScript={runFirst} />;
}
Btw, I am running the app on my iOS device if that helps you to answer.
Share Improve this question asked May 7, 2021 at 3:07 KLGKLG 1831 gold badge2 silver badges16 bronze badges 1- Can my answer help you? If yes, please accept the answer. If no, please provide more information for us to help you. – Mic Fung Commented May 8, 2021 at 9:01
2 Answers
Reset to default 6You should include onMessage={(event) => {}}
in the WebView
https://github./react-native-webview/react-native-webview/blob/master/docs/Guide.md
An onMessage event is required as well to inject the JavaScript code into the WebView.
<WebView
source={{ uri: "https://www.google./" }}
renderLoading={LoadingIndicatorView}
startInLoadingState={true}
javaScriptEnabled={true}
injectedJavaScript={runFirst}
onMessage={(event) => {}}
/>
try
injectedJavaScriptBeforeContentLoaded={injectedJavascript}
https://github./react-native-webview/react-native-webview/blob/4d8a76f3691479ef22b55e05c07921af99332395/docs/Guide.md#loading-local-html-files
本文标签: javascriptReact Native WebviewinjectJavascript not workingStack Overflow
版权声明:本文标题:javascript - React Native Webview - injectJavascript not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741316803a2371963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论