Reputation: 9922
I have to replace the protocol part of a already existing url in GWT. The java.net
package has a class which was build for exactly that purpose: URL. Sadly GWT does not emulate the java.net package.
How can I reassemble a url in GWT without creating my own parser? (I know about UrlBuilder, but UrlBuilder won't take an existing URL)
Example: I have a url in a string "http://myserver.com/somepath/file.html?param" and I want to replace the protocol part with "https".
Upvotes: 16
Views: 5928
Reputation: 9067
It's ugly, but you can always create an anchor element and extract the parts from there.
AnchorElement a = Document.get().createAnchorElement();
a.setHref("http://test.com/somerandompath");
Window.alert(a.getPropertyString("protocol") + " " + a.getPropertyString("host")) + " " a.getPropertyString("pathname"));
a.removeFromParent();
Upvotes: 6
Reputation: 7498
public void onModuleLoad() {
Button btn = new Button("change protocol");
btn.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
UrlBuilder builder = Window.Location.createUrlBuilder().setProtocol("https");
Window.Location.replace(builder.buildString());
}
});
RootPanel.get().add(btn);
}
Upvotes: 6
Reputation: 13620
Does Window.Location help you at all? You can read out the URL there, mod it and .assign()
it back.
Upvotes: 0