Search code examples
cordovaionic-frameworkionic2inappbrowser

How to use "executeScript" method of inAppBrowser plugin in ionic 2?


I have this code in a click event

let browser = new InAppBrowser('http://example.com/test', '_self');
browser.executeScript({code: "(function() { alert(123); })()"});

When i click the button, inAppBrowser opens the page, but the script doesn't run.
I closed the browser and then clicked the button again, now the script does run.

Why it's not running for the first time?


Solution

  • Add executeScript inside a InAppBrowser.addEventListener (loadstart, loadstop, loaderror) then it will run at the first time. I am using ionic 1 but I guess it will also work in ionic 2. I added a sample code below. (This is how it look like in ionic 1).

      browser.addEventListener('loadstart', function(){
                browser.executeScript({code: "(function() { alert(123); })()"});
            })