Skip to content

Instantly share code, notes, and snippets.

@schorfES
Last active December 26, 2015 12:29
Show Gist options
  • Save schorfES/7151152 to your computer and use it in GitHub Desktop.
Save schorfES/7151152 to your computer and use it in GitHub Desktop.
This detects if the application runs in iOS standalone mode and manually opens clicked links in webapps on homescreen to prevent switch to safari app. The code is designed to be used as backbone.geppetto command. See also: https://github.com/ModelN/backbone.geppetto#implementing-a-command
define(function(require) {
var
$ = require('jquery'),
Command = function() {}
;
/* This detects if the application runs in iOS standalone mode and
/* manually opens clicked links in webapp */
Command.prototype.execute = function() {
// Test if browser is in 'standalone'-mode:
if (window.navigator && window.navigator.standalone) {
// Use delegate to enshure that the eventHandler is the
// last to be executed:
$(document).delegate('a', 'click', function(event) {
// Only perform navigation when other eventHandler didn't
// already prevented the default task:
if (!event.isDefaultPrevented()) {
var location = event.currentTarget.href;
// Only open links in webapp when link is on same domain:
if (typeof location === 'string' && location.indexOf(window.location.host)) {
event.preventDefault();
window.location.href = location;
}
}
});
}
};
return Command;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment