javascript - Navigating to pushState URL -


i using window.history.pushstate set friendly-links articles. example there different categories articles , links become myweb.com/category/java/variables.

the problem pushstate cant bookmark them, getting 404 not found when navigating pushstate generated url 1 above.

im using java server-side without frameworks, , woudnt use if possible, doing things in javascript , heavy use of ajax jquery.

to solve this, used http://tuckey.org/urlrewrite/ url-rewrite library setting following:

<urlrewrite> <rule>     <from>^/intro</from>      <to type="redirect">http://localhost:8084/myweb.com/w?uri=intro</to>   </rule>   <rule>     <from>^/category/(.*)/(.*)</from>      <to type="redirect">http://localhost:8084/myweb.com/w?uri=category:$1:$2</to>  </rule> </urlrewrite> 

the servlet w simple thing, redirects index:

request.getrequestdispatcher("/index.jsp").forward(request, response); 

so when user navigates myweb.com/category/java/variables url rewriten http://localhost:8084/myweb.com/w?uri=category:java:variables, @ point on index.jsp, not on page bookmarked.

to solve run script when page loads:

href = window.location.href; href = href.replace(/:/g,'/'); if(contains(href, "w?uri=")){     pagerequest = href.split("=")[1];     urlmap(pagerequest); } 

this gets page of interest , forwards urlmap handles urls (i simplified next method, more generic):

function urlmap(href){     if(href==="category/java/variables"){     //here ajax request server content of article.      //and update navbar url      window.history.pushstate(state, title, "category/java/variables"); 

this works correctly there slight flash in browser navbar, when writing category/java/variables after pressing enter url turns w?uri=category:java:variables 100 miliseconds turns category/java/variables ... is there better way of doing this?

and few other questions make easier:

  1. is possible catch category/java/variables in javascript instead of server, or how pass in better way ?
  2. is possible make rules without redirection loop? this:

    <rule> <from>^/intro</from> <to type="redirect">http://localhost:8084/myweb.com/intro</to>    </rule> 

    or

    <rule> <from>^/category/(.*)/(.*)</from>  <to type="redirect">http://localhost:8084/myweb.com/category/$1/$2</to>   </rule> 

i map urls wanted, without flashes , strange url changes, did next:

1- change rules this:

<rule>     <from>^/intro</from>      <to>w?page=intro</to>   </rule>  <rule>     <from>^/([a-z]+)/([a-z]+)$</from>     <to>/w?category=$1&amp;article=$2</to> </rule> 

removed type="redirect" change navbar url. passes <to> url ghost request.

2- in servlet set cookie category=java , cookie post=variables. assuming from myweb.com/java/variables. forward index.

3- first thing when website loads tell javascript check cookie , url reference /java/variables pushstate new url, tough not necessary.


Comments