How to make a button link to another html page? - gwt

How to make a button link to another html page?
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//this code has to redirect to another page within the project
}

The question is tagged as GWT so based on that... to do it entirely on the client, you can use the com.google.gwt.user.client.Window class.
Window.open(linkURL, "_self", "");
Here's a link to the docs.

Look under gwt javadocs for the Window class.
There are some static members in the Window class.
The open function is the same as that in javascript:
Window.open(url, targetFrame, features )
In GWT, "_self" is the name of the current active display frame/window.
Location replace, just as in javascript:
Window.location.replace(url)
Display alert or confirm window:
Window.alert(msg)
Window.confirm(msg)

If you wanted a button but not in the script you can try this:
YOUR PAGE
Stack Overflow

No need to use JS. As of HTML5, buttons support the formaction attribute where you can specify the URL.
<form>
<button formaction="http://stackoverflow.com">Go to stackoverflow!</button>
</form>
Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction

Related

how to dianmically add dialog to webpage in wicket?

I have write my own dialog and on my Index page have a AjaxLink
when AjaxLink is clicked. I will behave to open my dialog instance.
this is my code
add(new AjaxLink("open.working.date.dialog") {
#Override
public void onClick(AjaxRequestTarget target) {
WorkingDateDialog dialog = new WorkingDateDialog("working.date.dialog", getString("index.working.date.dialog.title"), true);
Index.this.add(dialog);
dialog.open(target);
}
});
of course, on my web page html markup I don't have reference component id working.date.dialog and it will throw exception.
but when I replace Index.this.add(dialog); by this.add(dialog); or by target.add(dialog); the dialog won't work.
There any other way to add dynamically dialog to page?
In jquery I can do that easily by just append dialog html to body then open it by jquery.
thanks for your all helpful!
One option is to add the dialog in the constructor of the page and hide it initially. Then later when clicking the link just mark it as visible and add it to the AjaxRequestTarget.
Another option is to add a dummy/empty WebMarkupContainer with the same component id and later replace it with the dialog in #onClick().
You are going against the wicket way of doing things here, which will cause you much pain. :-) First of all WorkingDateDialog needs to extend ModalWindow. Assuming it does, here are my suggestions.
Right above your ajax link add this code:
final WorkingDateDialog dialog = new WorkingDateDialog("working.date.dialog", getString("index.working.date.dialog.title"), true);
add(dialog);
Then your ajax link becomes:
add(new AjaxLink("open.working.date.dialog") {
#Override
public void onClick(AjaxRequestTarget target) {
dialog.show(target);
}
});
So you always add your ModalWindow instance to your page hierarchy. It' just not visible until someone clicks the link.
I hope this helps you. Please let me know if you have any questions.

GWT: Opening new mail window without browser tab opened

I am trying to open an email client just like using mail me 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:john.doe#mail.com", "_blank", "");
}
}
Everything is working fine except one detail: When the widget is clicked, new empty browser tab will open with url set to mailto:john.doe#mail.com. 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.
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:john.doe#mail.com");
}
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

How to open an external web page from a Wicket modal dialog

I have a modal dialog in Wicket that contains a link. I need to open an external web page (for example, http://www.google.com) by clicking on the link. The target of the link is set dynamically. How can I do this?
I think that my question hasn't been so clear(I apologize for that). I need to open Web page from modal dialog. Actually, I can explain the problem in the example of modal dialog that #Don Roby has proposed me (wicketstuff.org/wicket14/ajax/modal-window.0). If we click the "Show modal dialog with a page" link in the example, there will be shown the modal dialog with another link called "Open another modal dialog". By clicking on that link, I want to open Web page (for example: www.google.com). My question is: how to open a Web page in this situation?
You can use a PageCreator (instead of setContent()), and return a RedirectPage:
ModalWindow modal = new ModalWindow("modal");
modal.setPageCreator(new ModalWindow.PageCreator() {
#Override
public Page createPage() {
return new RedirectPage("http://www.google.com");
}
});
add(modal);
I understand what you mean.
I have found the solution here :
http://apache-wicket.1842946.n4.nabble.com/How-to-redirect-from-a-ModalWindow-td1889646.html
onClick( AjaxRequestTarget target ){
target.appendJavascript( "Wicket.Window.unloadConfirmation = false;" );
modal.show( target);
}
It sounds like you already know how to deal with the modal, but there's an example of doing it here. Opening an external link is not difficult, and there's an example of doing it here.
how do you create your link ?
did you tried with an externalLink ?
new ExternalLink("applicationLink","http://www.google.com");

google wave: how did they make divs clickable

As we are facing GWT performance issues in a mobile app I peeked into Google Wave code since it is developed with GWT.
I thought that all the buttons there are widgets but if you look into generated HTML with firebug you see no onclick attribute set on clickable divs. I wonder how they achieve it having an element that issues click or mousedown events and seemingly neither being a widget nor injected with onclick attribute.
Being able to create such components would surely take me one step further to optimizing performance.
Thanks.
ps: wasnt google going to open source client code too. Have not been able to find it.
You don't have to put an onclick attribute on the HTML to make it have an onclick handler. This is a very simple example:
<div id="mydiv">Regular old div</div>
Then in script:
document.getElementById('mydiv').onclick = function() {
alert('hello!');
}
They wouldn't set the onclick property directly, it would have been set in the GWT code or via another Javascript library.
The GWT documentation shows how to create handlers within a GWT Java app:
public void anonClickHandlerExample() {
Button b = new Button("Click Me");
b.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// handle the click event
}
});
}
This will generate an HTML element and bind a click handler to it. However, in practice this has the same result as using document.getElementById('element').onclick() on an existing element in your page.
You can hook functions to the onclick event using JavaScript. Here's an example using jQuery:
$(document).ready(function(){
$("#div-id").click(function(){
/* Do something */
});
});
If you're interested in optimizing performance around this, you may need to investigate event delegation, depending on your situation.
A click event is generated for every DOM element within the Body. The event travels from the Body down to the element clicked (unless you are using Internet Explorer), hits the element clicked, and then bubbles back up. The event can be captured either through DOM element attributes, event handlers in the javascript, or attributes at any of the parent levels (the bubbling or capturing event triggers this).
I'd imagine they've just set it in a .js file.
Easily done with say jQuery with $(document).ready() for example.

load HTML when click a button GWT

how to load the other HTML page when i click a button? i'm using GWT 2.0.3.
p.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
// is there a syntax to load other HTML?
}
});
thanks before, Rafael.
If you use GWT's history mechanism, then you want to look at History.newItem("newPage");
If you want to jump to a totally new URL (and navigate outside your application) then you can use Window.Location.replace("newURL");.
Finally, if you just want to change the page within your application but are not using history, then you probably want to do something like:
RootPanel.get().clear()
RootPanel.get().add( widgetForNewPage );