Links can open new tabs, but button can't?

I have been able to open new tabs using Links, the setTarget(“_blank”) method. But When I want to open a page using getUI().getPage().open(“url”, “_blank”), the page opens in a new browser window! Is this a bug? I have also tried executing javascript with getUI().getPage().getJavaScript().execute(“window.open(urlhere)”, targethere, optionshere), and that also produces a popup instead of a new tab.

Hi, to open new browser tab usin button, use
com.vaadin.server.BrowserWindowOpener
to extend button.
Set target URL using method
setUriFragment
, after clicking on the button browser will open new tab.
In addition this approach will avoid pop-up blockers.

Hi could you give an example on how to extend button using BrowserWindowOpener?

One of examples is here:
https://vaadin.com/book/vaadin7/-/page/advanced.html

(googling the term should give you dozen of examples)

Also note that if you wish to have browser TAB opened instead of pop-up window, do not set any parmeters using method

setFeatures,
otherwise it will open pop-up.
Just set window name on your opener like this
opener.setWindowName(“_blank”);

Thanks i tried using it but am getting very odd behavior. When I click the button once, nothing happens. Then I click again and it will open a new tab with the correct UI and view I wanted. Then the third click will open both a new tab, and new popup.

I am using the BrowserWindowOpener within a sub class like this:

public class LinkButton extends Button
{
     public void setClickListener()
     {
        this.addClickListener(new Button.ClickListener(){
        
         BrowserWindowOpener opener = new BrowserWindowOpener("scheduleEvents.jsp");
         opener.extend(LinkButton.this);
         opener.setUriFragment("editwhatever");

         }


     }


}

Thats a simplified version. But the issue is that I want to set the constructor with this resource or url after I evaluate a certain condition. I tried instantiating the BrowserWindowOpener outside of this inner class, and it worked find, but I need to instantiate within this class because the url may be different depending on different conditions.

The problem is that you’re instantiating the BrowserWindowOpener inside the ClickListener. Instantiating it and extending the button must be done before the button was clicked.

It can be simple as this:

Button linkButton = new Button();

BrowserWindowOpener opener = new BrowserWindowOpener("scheduleEvents.jsp");
opener.extend(linkButton);
opener.setUriFragment("editwhatever");

you dont need listener at all if you want just to open new tab
you can set
opener.setUriFragment(“editwhatever”);
in other listener but not in the extended button’s listener

Thanks, I was hoping to avoid that so I could just keep the code in my extended button and use the logic thats in the click listener to decide what resource the opener would use, but I guess that will have to do.

When you want to use dynamic resources, try

Page.getCurrent().open(resource, "_blank", false);

It´s deprecated, but it´s the only way to achieve this without much effort.

I also tried that, but it would open in a new window. I used getUI().getPage().opent(“urlhere”, “_blank”, false);

I did some more research and found that in Javascript when you use window.open(), a new tab will only be opened if it is called from an onclick event. Using window.open() in a more programmattic way, i.e. calling it for a certain condition, javascript will open a new window.

Hey dre lo,
could you tell us please where you find this research results? I think it’s pure browser’s decision how to open a new tab/window.

Yes it’s up to the browser, but it seems that it also depends how the window.open() is being called. I found that here: http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript
The third answer on that page. But seems there is controversy on the answer.

I just know that it seemed inline with my results where I used a Vaadin Link, set the target to _blank, and Chrome will open a new tab. But if I use a button and set a click listener that will execute window.open(), with a target of _blank, Chrome opens a new popup.

ok, thank you! That is tricky :slight_smile: