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
V1nitV1nit 

Custom Button to close the window

I have created a custom button on a custom object called "Contact". which is of type "Onclick Execute JavaScript".

I have got "window.close()" in the button code.

 

I want to use this button on the Visualforce page.

Once the user clicks on the button, the browser window should close. Any ideas on how to use the custom button on the visualforce page ? 

bob_buzzardbob_buzzard

I can't see any way that you can access the custom links/buttons from the object definition, though I haven't looked that hard I must admit.

 

Is there any reason why you wouldn't just put a command button in the Visualforce page with onclick javascript?

V1nitV1nit

I tried that as well. But, can you please share the CommandButton code we use to close the window using window.close() function.

 

This is the piece of code I am using at the moment.

<apex:commandButton action="window.close()" value="Close" onclick="window.close()"/>

 

bob_buzzardbob_buzzard

You don't need the action attribute - that's only needed if you are executing a method in your controller.

 

The onclick should work when used that way - what behaviour are you seeing?

V1nitV1nit

Thanks for that. I removed the action attribute and tried clicking on the button......The page just refreshed....Didn't close the window. Any alternative way to do this ?

bob_buzzardbob_buzzard

Try:

 

window.close(); return false;

 

I think that the form submission will continue if there is no return value.

Darshan FarswanDarshan Farswan

window.close() works best when used with a newly created window having a particular new name. I suggest you to use self.close(). I tried the same but couldn't reach to what you were asking for.

sfdcfoxsfdcfox

There is a security restriction on JavaScript that prevents a window from closing unless opened by JavaScript. I know that previously they used to popup a dialog box, but now I think it's just plain ignored unless it was opened by a script.