Open new tab in dev mode with window.open

1,224 views
Skip to first unread message

Thomas Lefort

unread,
Sep 11, 2012, 10:08:46 AM9/11/12
to google-we...@googlegroups.com
Hi,

I am trying to programatically open a new tab in dev mode with a new instance of my application, eg my application url + the place name. I use window.open("#myplacename:", "_blank"). But all I get is a window without any of the specs/options such as navigation, etc... also the url is wrong, it links to the hosted.html file.

If I set the href and teh target of an anchor tag to the same values, there is no problem. It's really with the Windows.open that it fails. I also tried with a jsni, eg window.open(url, '_blank') but with the same issue.

If anybody has an idea of what could possibly be wrong and why it works with an anchor but not with window.open, it would be very much appreciated.

Thanks,

Thomas

Juan Pablo Gardella

unread,
Sep 11, 2012, 10:14:34 AM9/11/12
to google-we...@googlegroups.com
Did you tried use a invisible anchor and click on it?

2012/9/11 Thomas Lefort <lefor...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/IpENeXLy-4AJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Thomas Broyer

unread,
Sep 11, 2012, 10:37:55 AM9/11/12
to google-we...@googlegroups.com
How about Window.open(Window.Location.createUrlBuilder().setHash("#myplacename:").buildString(), "_blank", "") ?
(or using string manipulation on Window.Location.getHref() to append/replace the hash)

Thomas Lefort

unread,
Sep 11, 2012, 12:16:43 PM9/11/12
to google-we...@googlegroups.com
Hi,

Thanks to both of you.

Regarding teh anchor, I did try with the following code, but it still doesn't work.

Anchor anchor = new Anchor();
anchor.setHref("#EISearchResultPlace:" + result);
anchor.setTarget("_blank");
anchor.setVisible(false);
RootPanel.get().add(anchor);
anchor.fireEvent(new GwtEvent<ClickHandler>() {

@Override
public com.google.gwt.event.shared.GwtEvent.Type<ClickHandler> getAssociatedType() {
return ClickEvent.getType();
}

@Override
protected void dispatch(ClickHandler handler) {
handler.onClick(null);
}
});

I tried the URL trick from Thomas (1st suggestion) and it does work fine wrt the URL, so that's one porblem solved, thanks! but the window is still some stripped down version of my initial Chrome window and it is a window, not a tab...

Thomas Broyer

unread,
Sep 11, 2012, 12:43:14 PM9/11/12
to google-we...@googlegroups.com


On Tuesday, September 11, 2012 6:16:43 PM UTC+2, Thomas Lefort wrote:
Hi,

Thanks to both of you.

Regarding teh anchor, I did try with the following code, but it still doesn't work.

Anchor anchor = new Anchor();
anchor.setHref("#EISearchResultPlace:" + result);
anchor.setTarget("_blank");
anchor.setVisible(false);
RootPanel.get().add(anchor);
anchor.fireEvent(new GwtEvent<ClickHandler>() {

@Override
public com.google.gwt.event.shared.GwtEvent.Type<ClickHandler> getAssociatedType() {
return ClickEvent.getType();
}

@Override
protected void dispatch(ClickHandler handler) {
handler.onClick(null);
}
});

fireEvent() is a GWT-ism, it does not dispatch an event to the element; there's absolutely no event going on in the browser with fireEvent.
First, you should use an AnchorElement which is lighter-weight than an Anchor widget, then simply use JSNI to call the click() method (or, without JSNI: cast() to a ButtonElement)


I tried the URL trick from Thomas (1st suggestion) and it does work fine wrt the URL, so that's one porblem solved, thanks! but the window is still some stripped down version of my initial Chrome window and it is a window, not a tab...

You cannot force opening in a new tab or window, it all depends on the browser's configuration. In other words, it's at the user's discretion, and should stay that way (you'll find numerous discussions at the WHATWG or W3C HTML WG when people proposed adding a new _blankTab or similarly-named token, or new attributes on <a> to ask for a new tab rather than window, or control this through CSS, etc.)

Thomas Lefort

unread,
Sep 11, 2012, 3:54:24 PM9/11/12
to google-we...@googlegroups.com
fireEvent -> Oh I see, makes perfect sense! Thanks.

click -> I can't get it to work unfortunately, the object has no
'click' method, both using ButtonElement cast and JSNI
element.click(). I feel a bit useless there, if you have a little more
patience to detail your solution? Thanks.

tab -> I agree the browsers settings and behavior will define the
behavior. However in my case if I click on an anchor with teh _blank
target, it opens in a new tab. I am trying to reproduce the same
programatically but instead of getting a new tab I get a new window
with no navigation butttons or else, just the URL bar.

This is my code for the anchor

AnchorElement anchor = DOM.createAnchor().cast();
anchor.setHref("#EISearchResultPlace:" + result);
anchor.setTarget("_blank");
clickAnchor(anchor);

public static native void clickAnchor(Element element)/*-{
element.click();
}-*/;
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-web-toolkit/-/mjR6F7jYTIYJ.

Abraham Lin

unread,
Sep 11, 2012, 4:43:38 PM9/11/12
to google-we...@googlegroups.com
On Tuesday, September 11, 2012 3:54:55 PM UTC-4, Thomas Lefort wrote:
This is my code for the anchor

                                AnchorElement anchor = DOM.createAnchor().cast();
                                anchor.setHref("#EISearchResultPlace:" + result);
                                anchor.setTarget("_blank");
                                clickAnchor(anchor);

        public static native void clickAnchor(Element element)/*-{
                element.click();
        }-*/;

In both Firefox and Internet Explorer, it looks like you have to attach the element to the document first (Chrome doesn't seem to care).

You should also note that windows opened programmatically in this fashion will generally be blocked by built-in popup blockers.

-Abraham
Reply all
Reply to author
Forward
0 new messages