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

14,093

Solution 1

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); })()"});
        })

Solution 2

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

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

This is working for me.

Share:
14,093
RezaT1994
Author by

RezaT1994

Updated on June 11, 2022

Comments

  • RezaT1994
    RezaT1994 almost 2 years

    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?

  • rashadb
    rashadb about 7 years
    When I try to addEventListener it tells me that "Property 'addEventListener' does not exist on type 'InAppBrowser'. What's going on here?
  • coder
    coder about 7 years
    did you installe the cordova-plugin-inappbrowser?
  • rashadb
    rashadb about 7 years
    I did and that method is not a part of the Ionic wrapped version. I gained access to it by using the regular cordova version.
  • karthik elavan
    karthik elavan almost 4 years
    dear. team, we want to parse cookies to browser and we are developing the application ionic 3 based. please help us
  • karthik elavan
    karthik elavan almost 4 years
    we tried this.inAppBrowserInstance = this.inAppBrowser.create(URL, '_blank'); this.inAppBrowserInstance.on('beforeload') .subscribe(event => { this.inAppBrowserInstance.executeScript({ code: document.cookie = "JSESSIONID=cookievalue" }).then(). but not working
  • user276648
    user276648 over 2 years
    @rashadb With the Ionic wrapper, use on instead of addEventListener. See stackoverflow.com/a/47560757/276648