0

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>

3

In order to ensure that the link is opened in the same tab, you should use

window.location.replace()

See the example below:

window.location.replace("http://www.w3schools.com");

Source - http://www.w3schools.com/jsref/met_loc_replace.asp

0

You can use below syntax to open the url in the same page.

window.open("<URL>",'_self');

Check the window.open method and its parameters at https://www.w3schools.com/jsref/met_win_open.asp

Another solution would be to use replace as suggested by Sahil (upvoted). I cannot think of cons and pros of any approach so its upto you how you would like to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.