Created
September 25, 2014 12:40
-
-
Save martyychang/917efefdfa1a15b67f2d to your computer and use it in GitHub Desktop.
isSalesforce1() JavaScript function
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
/* | |
* @return whether JavaScript is executing | |
* within the context of Salesforce1. | |
* If not, the context is assumed | |
* to be the regular browser "app". | |
*/ | |
function isSalesforce1() { | |
// Use the presence of sforce.one | |
// as the acid test to know that | |
// the code is running in Salesforce1. | |
// Assume sforce.one is absent | |
// unless it's detected. | |
var hasSforceOne = false; | |
// Follow best practices recommended | |
// by Mozilla to test for presence | |
// of sforce.one: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined | |
if (typeof sforce !== 'undefined') { | |
if (typeof sforce.one !== 'undefined') | |
hasSforceOne = true; | |
} | |
// Return presence or absence | |
// of sforce.one as the result | |
return hasSforceOne; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment