+ Start a Discussion
dsh210@lehigh.edudsh210@lehigh.edu 

CommandButton target attribute

Hey all,

 

So I am using some command buttons to execute some page redirects via page references, but I was wondering how I could specify the target for the new window. I know that outputLink has the target attribute, but I would like to stick with the default Salesforce style on the buttons and not have to recreate it myself. Right now I have resorted to using javascript because I can specify "_parent", but does anyone know how to accomplish this with a PageReference?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
d3developerd3developer

 

 

 

<apex:commandButton action="{!saveRecord}" reRender="newWindow" onComplete="openWindow()" />

 

 

 

<apex:outputPanel id="newWindow">


<apex:outputPanel rendered="{!myRecord.property == desiredValue}">
function openWindow()
{
window.open("{!$System.Page.newPage}");

}
</apex:outputPanel>
<apex:outputPanel rendered="{!myRecord.property != desiredValue}">
function openWindow()
{

}
</apex:outputPanel>

</apex:outputPanel>

 

All Answers

gv007gv007

Example here is yours commandbutton code in

 

<apex:commandButton action="{!ab}" value="send" id="theButton"/>

 

 

In yours controller code write a method like this

 

public PageRefreance ab()

 

{

String url = null;

url = '\apex\yoursvfpage';

 

PageRefreance  p = null;

p = new PageRefreance(' url');

p.sendRedirect(true);

return p;

}

 

 

This is a sample you can improve it.

 

dsh210@lehigh.edudsh210@lehigh.edu

Okay so I have that working to open in the current window, but suppose I want a popup now, is there any way to change the target from "_parent" functionality to something along the lines of what "_blank" provides?

d3developerd3developer

 

 

 

<apex:commandButton action="{!saveRecord}" reRender="newWindow" onComplete="openWindow()" />

 

 

 

<apex:outputPanel id="newWindow">


<apex:outputPanel rendered="{!myRecord.property == desiredValue}">
function openWindow()
{
window.open("{!$System.Page.newPage}");

}
</apex:outputPanel>
<apex:outputPanel rendered="{!myRecord.property != desiredValue}">
function openWindow()
{

}
</apex:outputPanel>

</apex:outputPanel>

 

This was selected as the best answer
SoloSolo

Hello,

 

You can used javasript call directly in <apex:commandButton

 

Example:

 

    <apex:commandButton onclick="window.open('{!DestURL}');" value="Generate" />

 

 

baller4life7baller4life7

Thank you Solo!

Works like a charm!

Saleforce_newSaleforce_new

Hi Solo,

 

How do I find out the 'DestURL'? Please help me, Thanks!

SoloSolo

Hello!

 

DestURL is actually URL you want to be redirected after hitting the button

 

Regards!

 

 

Saleforce_newSaleforce_new

Thank you Solo!

Carli AndersonCarli Anderson
How do I use the DestURL, but have it open it in the same windown instead of opening a new window?
Douglas AyersDouglas Ayers
Hi @Carli,

To open the URL in the same page your are on, you can use the action="{!DestURL}" attribute or just modify @Solo's syntax from window.open(..) to be location.href=".."

Example (open in same window):
<apex:commandButton onclick="location.href='{!DestURL}';" value="Generate" />
or
<apex:commandButton action="{!DestURL}" value="Generate" />

Example (open in new window):
<apex:commandButton onclick="window.open('{!DestURL}');" value="Generate" />

Also, for best practice for creating URLs to visualforce pages, please see the URLFOR function documentation:
http://www.salesforce.com/docs/developer/pages/Content/pages_variables_functions.htm
http://www.salesforce.com/docs/developer/pages/Content/pages_compref_commandButton.htm
 
Harish RamachandruniHarish Ramachandruni
HI,



<apex:commandLink action="{!reDirect}" target="_blank" value="Save" id="theButton"/>


U can use targate _blank 


Regards ,
Harish.R