How open in a new Tab for PageReference?

How open in a new Tab for PageReference?

To open the URL in a new Tab for PageReference, kindly use <apex:commandLink/>.

Sample Code:

Visualforce page:

<apex:page controller="Sample">
    <apex:form >
        <apex:pageblock >
            <apex:commandlink action="{!openSearch}" target="_blank" style="text-decoration:none;">
                <apex:commandButton value="Switch"/>
            </apex:commandLink>
        </apex:pageblock>
    </apex:form>
</apex:page>

Apex Controller:

public class Sample {

    public Sample() { }

    public PageReference openSearch() {

        PageReference pageRef = new PageReference( 'https://www.google.com' );
        pageRef.setRedirect( true );
        return pageRef;

    }

}

Output:

Leave a Reply