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
YS1114YS1114 

window.close() not working from a VisualForce page?

Hello,

 

I have this simple VisualForce page:

 

 

<apex:page showHeader="false">
<input type='button' onclick="alert('close');window.close();" value='Close' />
</apex:page>

 

which is opened from another page using window.open(url...).

 

When I click the Close button, the onclick script does run (the alert shows up), but the popup isn't closed. Doing the same without the VisualForce APEX:PAGE tag works as expected.

 

Is there something in the markup added by VisualForce to the page that prevents it from closing? how can it be disabled?

 

Thanks,

YS

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This is browser behaviour, not VF.  Different versions of browsers allow different close behaviour, but if you want to work across all browsers, you need to close the window from the page that opened it.

 

In my implementation I have the following JavaScript methods in the controlling page (the page that the window is opened from):

 

var newWin=null; function openPopup(objecttype, name, id) { var url="/apex/LookupPopup?type=" + objecttype + "&namefield=" + name + "&idfield=" + id; newWin=window.open(url, 'Popup','height=500,width=400,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no'); newWin.focus(); return false; } function closePopup() { if (null!=newWin) { newWin.close(); } }

 

 

Then in the dynamically opened window, I use the following mechanism to close it:

 

 

function CloseWindow() { var winMain=window.opener; if (null==winMain) { winMain=window.parent.opener; } winMain.closePopup(); }

 

All Answers

bob_buzzardbob_buzzard

This is browser behaviour, not VF.  Different versions of browsers allow different close behaviour, but if you want to work across all browsers, you need to close the window from the page that opened it.

 

In my implementation I have the following JavaScript methods in the controlling page (the page that the window is opened from):

 

var newWin=null; function openPopup(objecttype, name, id) { var url="/apex/LookupPopup?type=" + objecttype + "&namefield=" + name + "&idfield=" + id; newWin=window.open(url, 'Popup','height=500,width=400,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no'); newWin.focus(); return false; } function closePopup() { if (null!=newWin) { newWin.close(); } }

 

 

Then in the dynamically opened window, I use the following mechanism to close it:

 

 

function CloseWindow() { var winMain=window.opener; if (null==winMain) { winMain=window.parent.opener; } winMain.closePopup(); }

 

This was selected as the best answer
YS1114YS1114

Thanks for your reply.

 

I have used your sample code verbatim, but it still does not work for me. After I sprinkeled some alerts, it looks like window.opener is null, and while window.parent.opener is not null, it does not have the closePopup() function defined (which means it is probably not the window that actually opened the dynamic window).

 

The same code works as expected when taken out of the context of SFDC (I tried in both IE and FF and both misbehave with SFDC and do well when using plain HTML).

 

Is it possible there's something I need to have in my popup APEX window (the apex/LookupPopup in your sample)?

bob_buzzardbob_buzzard

No, there's nothing more than that to it in my system.  Strange, as it works for me in IE6/7, Firefox 3 and Chrome.

 

 

YS1114YS1114

Indeed.

 

Anyway, one more test confirmed that you are correct (honestly, I never doubted that).

 

When I use a bare-bone version of your code:

 

The caller page

 

<apex:page >

<script>
var newWin=null;
function openPopup()
{
var url="/apex/PopupPage";
newWin=window.open(url, 'Popup','height=500,width=400,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
newWin.focus();

return false;
}


function closePopup()
{
if (null!=newWin)
{
newWin.close();
}
}
</script>

<input type="button" onclick="openPopup()" value="Open Popup" />

</apex:page>

 

 and the popup page

 

<apex:page > <script> function CloseWindow() { var winMain=window.opener; if (null==winMain) { winMain=window.parent.opener; } winMain.closePopup(); } </script> <input type="button" onclick="CloseWindow()" value="Close" /> </apex:page>

 

it works just fine.

I am yet to find where my problem is (the actual project is very big and is using YUI as its foundation), but it sure isn't VF or APEX (which is a relief, as it is the only component in my system which is not within my control).

 

Thanks again for your help, and for the excellent tip on how to close a popup regardless of the browser in use.

 

YS

 

 

nelloCnelloC
It's worth just mentioning here that the javascript function closePopup() seems to be a standard function in all Visualforce pages which is called on focus. I have used the code given in the solution here and found that in fact the parent page function closePopup() is also called whenever the parent page regains focus due to the inbuilt Visualforce call. This is useful and worth being aware of.
Message Edited by nelloC on 10-26-2009 09:32 AM
Big VBig V

Make sure that you are not working in development mode. It works fine otherwise

Rakesh KumarRakesh Kumar

Hello,

 

 

for closing a child window you can use only

 

window.top.close(); instead of window.close(); method

 

VF code will be

 

<apex:commandButton onClick="window.top.close();"  value="Close" />

 

RK

 

Rakesh ARakesh A
Hi @bob_buzzard and All

I am able to close the VF page by placing Button with Javascript "window.top.close(); instead of window.close();".. working with Both , But when I place this VF page in Communities the window is not closing.

Can you any one please help me in this.
Manohar SF.ax1874Manohar SF.ax1874
if this solution doesnt work, then here's another solution https://salesforce.stackexchange.com/questions/63421/close-window-oncomplete-not-working