Search code examples
javascriptgwtjsni

Gwt using JSNI to manipulate window.location.hash


What I am want to do is really simple. I have an gwt app with just one button, everytime I click the button, I want my window.location.hash changed.

here is what I have so far


private native void setLocationHash() /*-{
   var hash = window.location.hash;
   window.location.hash = hash+1;
}-*/;

So every time my button click event fires, that JSNI function would be called. Suppose before button click, my url is http://127.0.0.1:8888/GwtSampleApp.html?gwt.codesvr=127.0.0.1:9996#1 after clicking the button, my url SHALL be http://127.0.0.1:8888/GwtSampleApp.html?gwt.codesvr=127.0.0.1:9996#2

But so far, this is not working :(


Solution

  • You don't need to use JSNI as GWT has the History class for your purpose.

    History.newItem("you_hash_here");
    

    see Documentation