+ Start a Discussion
U JayU Jay 

How can close current window on a button click after action in salesforce?

How can close current window on a button click after action in salesforce?
Have a button on a page layout in a tab.
When i click the button , will open a new visualforce page in new window.
When i click action button, have to close current window .
How can i achieve it?
Any help?
My current code :-
<apex:page controller="CommitteePositionRoleChangeController" showHeader="False">
    <script>
        function closeWindow() {
            alert(1);
            window.close();
        }
    </script>
    <apex:form >
       
        <apex:outputPanel >
            <apex:outputText value="Select New Contact"/>
            <apex:inputField value="{!positionCommitteService.Contact__c}"/>
            <apex:commandButton value="OK" action="{!committeePositionRoleChangeAction}" rerender="statusMessagePanel" onComplete="closeWindow()"/>
        </apex:outputPanel>
        <apex:outputPanel id="statusMessagePanel">
            <apex:pageMessages rendered="{!showStatusMessage}"/>
        </apex:outputPanel>
    </apex:form>            
</apex:page>
Thanks in advance. :)
AshwaniAshwani
You javascript is corrent. Check in chrome console that you are having any error or not.

<apex:commandButton value="OK" action="{!committeePositionRoleChangeAction}" rerender="statusMessagePanel" onComplete="window.close();"/>
AshwaniAshwani
<script>
        function closeWindow() {
            alert(1);
            window.close();
            alert('window close has been invoked and title of window is: '+window.document.title);
        }
 </script>

U JayU Jay
Hi Batman,

  But it is not working for me :(
AshwaniAshwani
Can you check that you are getting any error or not in console. Otherwise verify title of window.
Sagar PareekSagar Pareek
Hi U jay,

I suspect using rerender and onComplete together is creating trouble for you.
Try removing rerender.
<apex:commandButton value="OK" action="{!committeePositionRoleChangeAction}"  onComplete="window.close();"/>

and that must working fine then.

U JayU Jay
No error on console .
Link showeing is : https://cs8.salesforce.com/apex/CommitteePositionRoleChangePage?cID=a0gL00000012LvY
Title :  Page editor - CommitteePositionRoleChangePage - Mozilla fire fox
And it should work in all browsers.
I have a doubt, whether we need to close it from where it is opened.?
I appeard when click a button on a tab.
I have another doubt :-

How can show an error message if a field in a tab is null near a button in the same tab on click it.othervice load a new url in a new window?
U JayU Jay
Sagar,
Tried with it too:-
<apex:commandButton value="OK" action="{!committeePositionRoleChangeAction}" onComplete="window.close();"/>
But same issue :( Not closing the same window :(
AshwaniAshwani
Hi U Jay,

On the page which is opened call this javascript function as.

<script>
        function closeWindow() {
            alert(1);
            window.self.close();
        }
    </script>