Radium
Radium

Reputation: 604

GWT: Opening new mail window without browser tab opened

I am trying to open an email client just like using <a href="mailto:[email protected]">mail me</a> tag.

But I want to use my custom widget, which is not hyperlink, anchor or so. I added a DOM handler to my widget to listen to clicks:

public class VContactWidget extends VHorizontalLayout implements ClickHandler {

private HandlerRegistration clickHandler;

public VContactWidget() {
    // added some content here
    clickHandler = addDomHandler(this, ClickEvent.getType());
}

@Override
public void onClick(ClickEvent event) {
    Window.open("mailto:[email protected]", "_blank", "");
}

}

Everything is working fine except one detail: When the widget is clicked, new empty browser tab will open with url set to mailto:[email protected]. I don't want the new tab opened. Can I avoid it somehow?

Note I set _blank parameter, as used in many examples. I also tried to use empty string or some other values as well. I looked into documentation, but didn't find anything useful. https://developer.mozilla.org/en-US/docs/Web/API/window.open

One solution may be to use Anchor, but my component is more complex, not just single <a> link.

Another detail to note may be application server - I am using Tomcat 7 now.

Upvotes: 1

Views: 1772

Answers (2)

Radium
Radium

Reputation: 604

Trying to fire native event on hidden Anchor programatically did not work for me. (Which does not mean it cannot be done.)

This is, how I actually solved my problem: Instead of Window.open(), I used following call:

public void onClick(ClickEvent event) {
    Window.Location.assign("mailto:[email protected]");
}

Upvotes: 1

Andrei Volgin
Andrei Volgin

Reputation: 41089

This is not something that you can control. Whether this link opens in a new tab or a new window depends on the browser settings and user preferences.

I don't think it will make a difference if you use an Anchor or Window.open. In either case, the behavior may be different in different browsers. Also remember that for some users a click on this link will open Outlook or Mail, while for other users it will open Gmail in a browser window.

UPDATE:

If you want an exact behavior of an <a> element, create a hidden anchor element and fire a click on it when a user clicks on your composite widget.

Firing click event from code in gwt

Upvotes: 0

Related Questions