How do we open a pagereference object in a new window?

I cant seem to find anything on the documentation for opening in new window.

I need to open up a new page on click of a commandbutton. Is that possible?

Here is the commandbutton code i am using


<apex:commandButton value="Generate Pdf" action="{!generatePdf}" />

I'd say "you don't".

In the end PageReference will be an URL and it's up to browser how will it open it. So - if anything - you control it in Visualforce, not in Apex. Can you post your commandButton's definition?

Do you need to submit some data to the server and then open up a new window? In that case maybe some kind of <apex:commandButton oncomplete="..."> will work. If you need to "go to this page or another" - maybe actionFunction that'd be examining the returned PageReference? Is it more of onclick or oncomplete scenario anyway?

If it's a straightforward "just open me a new window" - maybe you don't need the whole action and parameters passed? In that case a plain outputLink or even <a> tag will work (styled with CSS class "btn" for good measure). Links don't pass params like buttons do though; check Why does apex:param assignTo work with apex:commandLink but not apex:commandButton? for workarounds.

EDIT after some explanation in comments

Experiment with this code sample:

            <apex:commandLink value="Generate PDF (new tab most likely)" action="/apex/testpdfpage" styleClass="btn" immediate="true"
                target="_blank" style="text-decoration:none" />
            <a href="/apex/testpdfpage" class="btn" style="text-decoration:none" 
                onclick="window.open(this.href, 'mywin', 'height=400,width=300'); return false;" >Pure HTML version, should be a popup</a>

It seeems that target="_blank" is ignored (Chrome and Firefox will spawn new tab instead of new window) so you need Javascript & window.open. I've included commandLink and plain anchor tag, you can of course mix & match them with onclick etc or use outputLink even...

Also probably a reference to {!$Page.testpdfpage} will be a bit better as if you'll ever try to delete the page SF will stop you saying that this page is used somewhere.

  • My basic need is to open up a pdf which i am generating in a new window. I also need to pass in parameters which i am currently doing using pagereference. Added the commandbutton code in the question
    – Prady
    May 8 '13 at 16:58
  • And do you really somehow handcraft the PDF? If you simply redirect to some page that has renderAs="pdf" or use Page.getContentAsPdf() in Apex then you could just link directly to it?
    – eyescream
    May 8 '13 at 17:03
  • i do use renderAs="pdf"
    – Prady
    May 9 '13 at 2:14
  • i am passing parameters too.. so do you think its better i build the url with parameters and pass it out to command link. right now i am adding parameters using the pagereference class
    – Prady
    May 9 '13 at 7:05
  • I tend to build my URLs in VF if I can. Easier to change in future (no need to deploy the class if all you did is forgot to urlencode something for example), less unit tests to write... Unless you do some wicked queries to fetch your params I'd say URLFOR is as flexible & easy to maintain as Page.getParameters. But that's a personal preference. Bottom line - for a window popup use JS as in nivyaj's answer and in my <a> example.
    – eyescream
    May 9 '13 at 7:14


 <apex:commandButton id="btn" value="label" onclick="window.open('//www.google.com','_blank','height=400,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1);"/>
  • That works for outputlink, but I don't think you can specify target= on a commandbutton. May 8 '13 at 15:13
  • updated for a btn May 8 '13 at 15:21

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.