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
Shravan Kumar 71Shravan Kumar 71 

Open Link in the same window below button in VF page

I am looking for a solution where when I click on a button, the resulting page should open in the same window below the button. Below is my current VF page, right now the page is opening in a new tab.
 
<apex:page lightningStylesheets="true">
<style>
    body input.btn, body input.btnDisabled, body input.btnCancel {
    padding: 4px 3px;
    padding-left: 1rem;
    padding-right: 1rem;
    text-align: center;
    vertical-align: middle;
    justify-content: center;
    border: 1px solid #dddbda;
    transition: border .15s linear;
    background-color: #0070d2;
    border-color: #0070d2;
    /* color: #fff; */
    width: 300px;
    height: 35px;
    }
</style>

<apex:form target="_self" >

        <center>
            <apex:commandButton id="one" value="Lead Page" oncomplete="openDashboard(this.id);" />&nbsp;&nbsp;
            <apex:commandButton id="two" value="Account Page" oncomplete="openDashboard(this.id);"/> 
        </center>
        <br/>
</apex:form>

<script type="text/javascript">
function openDashboard(clicked_id) {
    if(clicked_id.includes('one')){
        window.open("Lead URL");
    }else if(clicked_id.includes('two')){
        window.open("Account URL");
    } 
}
</script>
Lokesh KumarLokesh Kumar
Sharvan, Change target from "_self" to "_parent" in the form tag.
Shravan Kumar 71Shravan Kumar 71
That didn't help. The page is opening in the new tab. 
Deepali KulshresthaDeepali Kulshrestha
Hi Shravan,

I have gone through your problem please refer below code:
Visualforce page:-

<apex:page lightningStylesheets="true">
    <style>
        body input.btn, body input.btnDisabled, body input.btnCancel {
        padding: 4px 3px;
        padding-left: 1rem;
        padding-right: 1rem;
        text-align: center;
        vertical-align: middle;
        justify-content: center;
        border: 1px solid #dddbda;
        transition: border .15s linear;
        background-color: #0070d2;
        border-color: #0070d2;
        /* color: #fff; */
        width: 300px;
        height: 35px;
        }
    </style>
    
    <apex:form target="_blank" >
        
        <center>
            <apex:commandButton id="one" value="Lead Page" oncomplete="openDashboard(this.id);" />&nbsp;&nbsp;
            <apex:commandButton id="two" value="Account Page" oncomplete="openDashboard(this.id);"/> 
        </center>
        <br/>
    </apex:form>
    
    <script type="text/javascript">
    function openDashboard(clicked_id) {
        if(clicked_id.includes('one')){
            window.open("Lead URL","_self");
        }else if(clicked_id.includes('two')){
            window.open("Account URL","_self");
        } 
    }
    </script>
</apex:page>


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Shravan Kumar 71Shravan Kumar 71
Hello Deepali,

I am looking at opening the page in the same window below the button. The above solution opens the page in the same window but I can't see the button. I have to go back if I want to click on other button. 

Thanks,
Shravan