Search code examples
salesforcevisualforceapex

VF standard cancel button either works on classic or Lightning Experience but not on both versions


I have a visualforce page with save and cancel buttons and this form I’m opening in tab. Ideally what I would like to do is when I click on cancel should clear the form and close the newly opened window itself.

<apex:commandButton value=“cancel” action="{!cancel}" oncomplete=“window.close();” />

This above statement working(window closing) fine in lightning experience but not on classic version.

<apex:commandButton value=“cancel” action=“{!Mycancel}" oncomplete=“window.close();” />

In above statement, I’m writing custom cancel(basically redirecting) and it closes the window in classic but this does not work in Lightning Experience.

I want a solution should work in classic as well as in Lightning Experience. Please help me out here.


Solution

  • You can detect whether it's Classic or lightning in javascript

     if(document.referrer.indexOf(".lightning.force.com") > 0){
            alert("This is lightning ");
        }else{
            alert("This is classic");
        }
    

    Check this out as well http://bobbuzzard.blogspot.com/2015/11/context-is-everything.html