Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native Access WebView Actions #59

Closed
YangYouYong opened this issue Feb 25, 2016 · 4 comments
Closed

React Native Access WebView Actions #59

YangYouYong opened this issue Feb 25, 2016 · 4 comments

Comments

@YangYouYong
Copy link

How can I access webview function in react native When I use your plugin?
In my webview when i tapped a button , it will calls a function named onClick , I want to response to this function in react native . so what code should i write in my script? Can you give me any suggestion? , I try to inject a JavaScript like this

var myPageScripts = function() {
  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);
  }
  WebViewBridgeReady(function (WebViewBridge) {
    WebViewBirdge.onMessage = {
      onClick : function() {
        WebViewBirdge.send("onClick")
      }
    }
  });
};
myPageScripts = `(${myPageScripts.toString()}());`;
@alinz
Copy link
Owner

alinz commented Feb 25, 2016

@YangYouYong I do have an example with latest react-native that showcases the message passing between webview and react-native. Take a look at this one and if you need more help let me know.

@YangYouYong
Copy link
Author

@alinz help me eg:test.html

<!DOCTYPE html>
    <html>
    <head>
    <script>

    function clicked() {
        alert("clicked button");
    }
</script>
</head>

<body >
<button onclick="clicked()">Touch me</button>
</body>
</html>

How can I write the inject JavaScript code to response to the function clicked() called in test.html ?

@YangYouYong
Copy link
Author

@alinz I solve it by change my test.html like this :

<!DOCTYPE html>
    <html>
    <head>
    <script>

    function clicked() {
         window.WebViewBridge.send('doSomething')
    }
</script>
</head>

<body >
<button onclick="clicked()">Touch me</button>
</body>
</html>

@alinz
Copy link
Owner

alinz commented Mar 5, 2016

@YangYouYong, Nice. Since you have managed to solve your problem I'm closing this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants