Solvedreact 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?

16 Answers

✔️Accepted Answer

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');
`;

Related Issues:

9
react native webview bridge [Android] injectScript not work!
I experienced something interesting My script works on iOS but applying it to android did not work ...