function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Angie ChuaAngie Chua 

sforce.one undefined in Winter 16 Lightning

Hi,
I need to distinguish whether the user is in Classic vs Lightning mode. I am following the suggestion in trailhead topic: Sharing Visualforce
Pages Between Classic and Lightning Experience which is the javascript code below. I am using Winter 16 Lightning mode. However, sforce.one shows up undefined.  Am I missing anything?  Has anyone encountered this?  Thanks.

function isLightningExperienceOrSalesforce1() {
    return((typeof sforce != 'undefined') && sforce && (!!sforce.one));
}
if( isLightningExperienceOrSalesforce1() ) {
    // Do something for Lightning Experience
}
else {
    // Use classic Visualforce
}
James LoghryJames Loghry
Check your developer console in Chrome or other browser for any javascript related errors.

For instance, it doesn't look like there's a space between return and ( in your example, which could lead the issue you're noticing.
Amit GoyalAmit Goyal
Hi Angie,

I used the same code which we have in trailhead to apply on our visualforce page.
It works as we need to apply this section inside a <script> tag.
Check the follwing code I have development in Lightning Experience mode and tested. match with your code.

<apex:page >
    <script type="text/javascript">
        function isLightningExperienceOrSalesforce1() {
            return((typeof sforce != 'undefined') && sforce && (!!sforce.one));
        }
        
        if( isLightningExperienceOrSalesforce1() ) {
            console.log('### Lightning Experience');
        } else {
            console.log('### Salesforce Classic');
        }
    </script>
</apex:page>

Please mark this answer if it solved your issue.

Thanks.
Angie ChuaAngie Chua
Thank you for the replies. Thanks Amit for confirming that the code works. 
 
We have existing code in Classic that we're updating to work for both Lightning and Classic.  I found out that the problem was caused by a prior step. The page (VFpageB) where I placed the javascript code, is called from another page (VFpageA) through window.location.href.  VFPageB loads fine and UI shows Lightning mode.  But after the call, sforce.one becomes undefined, making the javascript code in VFPageB evaluate to Classic mode.
 
If VFPageB is tested on its own through Tab (ref: https://developer.salesforce.com/trailhead/lex_dev_visualforce/lex_dev_visualforce_process) the javascript code evaluates fine.
 
To fix, I updated VFPageA to use sforce.one.navigateToURL, now the javascript code evaluates correctly on VFPageB. Thanks.