Last active
December 26, 2015 12:29
-
-
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
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
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