How to open in new window using pagereference?

  • 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}" />
  • eyescream

    eyescream Correct answer

    8 years ago

    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=", '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 & 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

    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?

    i do use renderAs="pdf"

    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

    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 `` example.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used