Skip to content
New issue

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

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

Already on GitHub? # to your account

It's possible to add a global variable(window.xxx) before page loaded? #207

Closed
Doflatango opened this issue Dec 3, 2019 · 3 comments
Closed
Labels

Comments

@Doflatango
Copy link
Contributor

Description

What you'd like to happen:

I want to add a global variable like window.flutter_inappwebview before page loaded, therefore, the script can use this variable once the page loaded.

Currently, i call InAppWebViewController.evaluateJavascript on event onLoadStart to do this, by it is too late, because the script in the page throw an error xxx is not defined.

@pichillilorenzo
Copy link
Owner

Why you can't use window.flutter_inappwebview.callHandler? There is a particular case?

You can simply add a javascript handler with controller.addJavaScriptHandler and, on the javascript side, just listen the flutterInAppWebViewPlatformReady global event with window.addEventListener("flutterInAppWebViewPlatformReady", function(event) { }) method and your handler. With this, you are sure that your handlers are defined.

For example:

  • Flutter side:
onWebViewCreated: (InAppWebViewController controller) {
  webView = controller;

  controller.addJavaScriptHandler(handlerName: "mySum", callback: (args) {
    // Here you receive all the arguments from the JavaScript side 
    // that is a List<dynamic>
    print("From the JavaScript side:");
    print(args);
    return args.reduce((curr, next) => curr + next);
  });
},
  • Javascript side:
... 

<script>
    // In order to call window.flutter_inappwebview.callHandler(handlerName <String>, ...args) 
    // properly, you need to wait and listen the JavaScript event flutterInAppWebViewPlatformReady. 
    // This event will be dispatched as soon as the platform (Android or iOS) is ready to handle the callHandler method. 
    window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
      // call flutter handler with name 'mySum' and pass one or more arguments
      window.flutter_inappwebview.callHandler('mySum', 12, 2, 50).then(function(result) {
        // get result from Flutter side. It will be the number 64.
        console.log(result);
      });
    });
</script>

...

This was referenced Jul 6, 2020
Copy link

github-actions bot commented Oct 7, 2024

This issue is stale and has been automatically closed because it has been open for more than 365 days with no activity. Please reopen a new issue if you still have it.

@github-actions github-actions bot added the stale label Oct 7, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 22, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants