Sign up ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a Visualforce page that is used in both Salesforce1 and the desktop. On that page is a button. That button should only display if the user is loading the page on the desktop. It should be hidden on Salesforce1. Any suggestions?

Essentially I am just looking for a way to detect if the Visualforce page is loaded in Salesforce1 or not. I can handle the logic of displaying the button, I just don't know if it is possible to tell if it is Salesforce1 or not.

share|improve this question

4 Answers 4

up vote 19 down vote accepted

If you can make due with JavaScript checking you can use the method outlined in this documentation that looks for the sforce JavaScript object, and just adapt it to do your work.

if( (typeof sforce != 'undefined') && (sforce != null) ) {
    // do your SF1 mobile button stuff
else {
    // do your desktop stuff

There isn't an official way to do a server side check. See this twitter conversation for more, where the following gist by TehNrd (Jason Venable) is referenced which checks for the presence of certain parameters.

//Determine if this is SF1 app
public static Boolean isSF1(){
    if(String.isNotBlank(ApexPages.currentPage().getParameters().get('sfdcIFrameHost')) ||
        String.isNotBlank(ApexPages.currentPage().getParameters().get('sfdcIFrameOrigin')) ||
        ApexPages.currentPage().getParameters().get('isdtp') == 'p1' ||
        (ApexPages.currentPage().getParameters().get('retURL') != null && ApexPages.currentPage().getParameters().get('retURL').contains('projectone') )
        return true;
        return false;

There is also an Idea,"Provide a supported url param to determine if page is in SF1", on the Idea Exchange.

share|improve this answer
Peter - would the best/simplest way to make a non-SF1 page compatible be to use the If/Else to split the page into two paths? One that has the legacy VF page details with the other being the SF1 view? – 44f May 29 '14 at 10:26
@44f - It would depend on the requirements for me. It could be cleaner to do something like two includes or just redirects and share the controller from the two different views (i.e., pages). – Peter Knolle May 30 '14 at 11:29
Reusing same VF page in desktop and s1 is fine, but what about too many tabs issue i.e. if a VF page is meant for mobile only and has no utility in desktop. I think we can promote an idea, to hide all such mobile only tabs from desktop. Plus, an option to make a tab available in both mobile + desktop would be nice. – Abhinav Gupta Jul 31 '14 at 13:02
For javascript you should really be using the !== operator to check for exact equivalence (which is what you want in this case). It's a bit faster since it doesn't attempt to do any type coercion. – Akrikos Mar 13 at 14:49

In addition to the officially sanctioned methods above, I wanted to add a way to check if the user is on a mobile device using user agent detection in apex:

public static boolean isMobile(String userAgentString) {
    //Using RegEx, figure out if the user is on a mobile device based on the user-agent string
    if (userAgentString == null)  return false;
    Pattern p = Pattern.compile('Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune');
    Matcher pm = p.matcher( userAgentString );

    return pm.find();

share|improve this answer

Try this:

var isSF1=typeof sforce === 'undefined'?false:true;

isSF1 will be a boolean indicating whether you're loaded in SF1.

share|improve this answer

As Peter notes in his answer, Salesforce provides documentation for how to do this on the front end. However, their javascript can be simplified a bunch:

if (sforce) {
    // do your SF1 mobile button stuff
else {
    // do your desktop stuff

Any undefined or null variable is falsey.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.