Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 379
RezaT1994 Avatar asked Feb 21 '17 08:02

RezaT1994


2 Answers

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); })()"});
        })
like image 108
coder Avatar answered Oct 27 '22 07:10

coder


It's not working in this way anymore. Try it!

browser.on('loadstop').subscribe(() => {
  browser.executeScript({code: 'your_code'})
});

This is working for me.

like image 30
Andrews Duarte Avatar answered Oct 27 '22 07:10

Andrews Duarte