is there a way to detect if I stay in Salesforce Classic or Salesforce Lightning context from javascript as static resource?

I need this because before to detect the user theme I used

if(sforce && sforce.console && sforce.console.isInConsole())

but for some months now it doesn't work more because sforce can be used also in Lightning.

Do you know another trick to detect theme from JS?

2 Answers 2


In short, no, a static resource script cannot, by itself, reliably determine the current theme setting. You would need cooperation from some sort of outside source. For example, you might write an Apex Class to be called as a web service, import the AJAX toolkit, and call sforce.apex.execute, or call a Visualforce remote method, or write a Visualforce page that you call out to in order to get the value of {!$User.UIThemeDisplayed}. All four methods will require that you set up at least one class or Visualforce page to get the desired effect.


I found the solution:

if(UITheme.getUITheme() === 'Theme3')

The line above does the magic from a Javascript static resource! It is added in Summer '18, see here.

  • 1
    I used the function directly in my javascript file of lightning web component but it gave me 'UITheme' is not defined. error. What should I do differently?
    – Koustubh
    Nov 5, 2019 at 6:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .