Last active
March 20, 2019 11:58
-
-
Save KaiCMueller/73d80d047384a80c0da9a04ed9321d25 to your computer and use it in GitHub Desktop.
Cordova app which loads a website in full screen and opens external links in system browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var appUrl = 'https://www.my-website.com'; | |
var app = { | |
currentUrl: appUrl, | |
initialize: function() { | |
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false); | |
}, | |
onDeviceReady: function() { | |
app.openBrowser(); | |
}, | |
openBrowser: function() { | |
var target = '_blank'; | |
var options = [ | |
"location=no", | |
"clearcache=no", | |
"clearsessioncache=no", | |
"footer=no", | |
"hardwareback=no", | |
"hideurlbar=yes", | |
"zoom=no", | |
"hidenavigationbuttons=no", | |
"toolbar=yes", | |
"hidespinner=yes", | |
"toolbarcolor=#000000", | |
"toolbartranslucent=no", | |
"navigationbuttoncolor=#ffffff", | |
"closebuttoncolor=#000000", | |
"closebuttoncaption=" | |
]; | |
browser = cordova.InAppBrowser.open(app.currentUrl, target, options.join()); | |
// reopen the browser if it was closed via "Done" button | |
browser.addEventListener('exit', function(){ app.openBrowser(); } ); | |
// open external urls in system browser window | |
browser.addEventListener('loadstart', function(event){ app.handleUrl(event.url, browser); }); | |
}, | |
handleUrl: function(url, browser) { | |
var extension = url.split('.').pop(); | |
// load internal urls that are not pdf files in the app browser | |
if (extension != 'pdf' && url.startsWith(appUrl)) { | |
app.currentUrl = url; | |
return; | |
} | |
// open website in system browser window | |
var ref = cordova.InAppBrowser.open(url, '_system', 'location=no'); | |
// since the inapp browser is loading the website too, close it and open it again with the last internal url visited | |
browser.close(); | |
app.openBrowser(); | |
return; | |
} | |
}; | |
app.initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment