Skip to content

Instantly share code, notes, and snippets.

@martyychang
Created September 25, 2014 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save martyychang/917efefdfa1a15b67f2d to your computer and use it in GitHub Desktop.
Save martyychang/917efefdfa1a15b67f2d to your computer and use it in GitHub Desktop.
isSalesforce1() JavaScript function
/*
* @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