question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

injectedJavaScript is not working in IOS

See original GitHub issue

Bug description: I am using “react-native-webview”: “^10.10.2”. I am using “injectedJavaScript” in webview, this property is working fine in android but not working in ios.

Code import React, { Component } from ‘react’; import { WebView } from ‘react-native-webview’

class App extends React.Component { constructor(props) { super(props);

}

componentDidMount(){

}

myScript = () => window.document.getElementsByTagName("header")[0].style.display = "none";

render() { return ( <WebView source={{ uri: “https://www.helpmefund.org/campaigns” }} injectedJavaScript={this.myScript()} /> ); } }

export default App;

Environment:

  • OS:IOS
  • OS version:14.2
  • react-native version:0.63.3
  • react-native-webview version:10.10.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

25reactions
TimJungcommented, Dec 12, 2020

Hey @sraza295 I was able to fix the issue I was having by adding: onMessage={(event) => {}} as a prop to the WebView. Solution found here: https://github.com/react-native-webview/react-native-webview/blob/4d8a76f3691479ef22b55e05c07921af99332395/docs/Guide.md under “Communicating between JS and Native”.

Not sure why or when that became required because it definitely used to work without it. But perhaps that’s the solution for you as well.

5reactions
potatoengineercommented, Apr 2, 2021

I have the same issue, javascript isn’t being injected on iOS. I made sure to add true; to my script and include onMessage

...
    const SCRIPT = `
    const meta = document.createElement('meta');
    meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0');
    meta.setAttribute('name', 'viewport');
    document.head.appendChild(meta);
    true;
    `;
    return <WebView source={{uri: myURL}} javaScriptEnabled onMessage={(event) => {}} 
    injectedJavaScript={SCRIPT} />
Read more comments on GitHub >

github_iconTop Results From Across the Web

injectedJavaScript is not working in Webview of react native
Well, you have more than one issue here. First one is your web view needs to be wrapped in a containing View component...
Read more >
why react native webview injectedJavascript doesn't work on ...
Coding example for the question why react native webview injectedJavascript doesn't work on IOS?-React Native.
Read more >
Need help with react-native Webview, injecting ...
I figured out the answer. It's really nuanced. Essentially you have to create a global window variable before the injectedJavascript() method is ...
Read more >
React Native WebView: A complete guide
This package will work both on Android and iOS devices. ... Well, React Native WebView also offers a solution for this problem!
Read more >
Tool shows JavaScript code injected via in-app browser
Developer creates tool that shows injected JavaScript commands through an ... these in-app browsers are based on Safari's WebKit on iOS, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found