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.
Author by
RezaT1994
Updated on June 11, 2022Comments
-
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 about 7 yearsWhen I try to addEventListener it tells me that "Property 'addEventListener' does not exist on type 'InAppBrowser'. What's going on here?
-
coder about 7 yearsdid you installe the cordova-plugin-inappbrowser?
-
rashadb about 7 yearsI 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 almost 4 yearsdear. team, we want to parse cookies to browser and we are developing the application ionic 3 based. please help us
-
karthik elavan almost 4 yearswe 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 over 2 years@rashadb With the Ionic wrapper, use
on
instead ofaddEventListener
. See stackoverflow.com/a/47560757/276648