Reputation: 603
Im implementing a jQuery widget on my app. I want that on load, the widget starts, and to make this i have to call my url with one parameter.
So, it would be like this MYURL/?param=value
Im doing this right now:
Window.Location.replace(GWT.getHostPageBaseURL()+"?param=value");
And works fine, the problem is that reloads the app.
Is there a way to do this without refreshing my webpage?
Thank!
Upvotes: 1
Views: 4119
Reputation: 3652
Use Window.location is not a proper way to do this as Philippe Beaudoin (i think he works for Google) said:
From what I understand, modifying the browser URL without inserting a token in the history is not supported by browsers. GWTP 0.4 will not touch the URL (whereas 0.3 touched it, forcing you to insert a new element in the history). Calling updateHistory() should have the exact same effect as 0.3.
I remember some people saying that they were able to modify the URL without inserting an history token and without reloading the app by calling Window.Location.replace() using the current url with a modified hash fragment. However, this is not documented and I would be surprised if it worked on all browsers.
Source: https://groups.google.com/forum/#!topic/gwt-platform/glnowOS_8CM
In GWTP, you can do this very easily.
In GWTP, when you use eClipse to create a presenter say FirstPresenter, then eClipse will generate all necessary classes for you, these classes are: FirstPresenter.java, FirstView.java, FirstView.ui.xml
There some package (urproject.client.place) in Gwtp that help to manage all things to Place such as the param access, add mor param without refreshing the page.
In FirstPresenter.java, u first need to inject PlaceManager (put it outside the method) & then create a function saying addParam(String myParam):
@Inject PlaceManager placeManager;
public void addParam(String myParam, String myValue){
PlaceRequest request = new PlaceRequest(NameTokens.yourNameToken).with(myParam, myValue);
placeManager.updateHistory(request, true);
}
Note:If you want >1 param, u can use as many .with
as u want .with(myParam, myValue).with(myParam2, myValue2)
. You can get param by String val=request.getParameter(myParam,"");
. If you want to get param when the page got initialized then use this:
@Override
public void prepareFromRequest(PlaceRequest request){
super.prepareFromRequest(request);
String myVal=request.getParameter("myParam", "");
}
That done. You must use Gwtp package to be able to use that function, Gwtp is the best framework i can suggest, they have a lot of powerful stuffs. You can't manage a very complicated web app by just using Gwt as Gwt is too limited & just for small app, but big web app require powerful framework & that is Gwtp. Not many people in Stackoverflow know Gwtp, so it hard to get answer but u can learn it by watching videos.
There're a very good videos for u to learn GWTP at youtube here:
http://www.youtube.com/watch?v=Gm-RO-cmsEQ&list=PL29DDDC847F63AF82
Hope this can help u
Upvotes: 1
Reputation: 9741
In order not to reload the page, you have to modify just the hash fragment instead of the query string.
Window.Location.replace(
Window.Location.getPath() + Window.Location.getQueryString() +"#whatever");
In most browsers, though, there is a new mechanism called pushState so as you can change the url to whatever value without reloading the page. GWT does not have any wrapper for this yet, but you can use it through JSNI or, if you prefer to add gwtquery to your project, using this code:
import static com.google.gwt.query.client.GQuery.*;
Properties history = JsUtils.prop(window, "history");
JsUtils.runJavascriptFunction(history,
"pushState", null, null, "whatever.html?foo=true");
FYI, the new gwtproject web site is using this mechanism and gwtquery when navigating through the menu. The source code is in gwt git.
Upvotes: 4