react-native-webview-bridge: [Android] injectScript not work!

I write bellow code:

function webViewBridgeReady(cb) { if (window.WebViewBridge) { cb(window.WebViewBridge); return; }

function handler() {
  document.removeEventListener('WebViewBridge', handler, false);
  cb(window.WebViewBridge);
}
document.addEventListener('WebViewBridge', handler, false);

}

function goDetail(id) { WebViewBridge.send(“jump:” + id); }

webViewBridgeReady(function (webViewBridge) { webViewBridge.send(“message from webview”); });

the html is get from server, html have a button and click to invoke the goDetail function,but in android the log cat say ‘goDetail is not defined’. can you help me?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

I experienced something interesting.

My script works on iOS but applying it to android did not work.

  • first as @g6ling suggested watch example
  • secondthat is what is interestingerase all javascript comments in your scripts!!!
    • it broke my script on android!!! It was why it did not work…

Illustration:

const scriptNotWorking = `
 // a comment <= erase all comments like this one!!!
 alert('it won\'t work');
`;
const scriptWorking = `
 alert('it will work');
`;

I got the same issue. The code works on iOS buy didn’t work on Android even the simplest code as MacKentoch’s.

I also am unable to inject JS into a WebViewBridge on android. If I change the type to plain old WebView with all the same props then it works so I suspect it must be something with this library.

Further, I’m seeing the same weird issue with comments as @MacKentoch but I’m seeing it with the plain old WebView. Any comments at all (even at the end of the line or at the end of all the JS) cause the javascript to not run.