JQuery Ajax adding hash in the url JQuery Ajax adding hash in the url ajax ajax

JQuery Ajax adding hash in the url


Here is a working example (not considering Struts2):

<html><body><a id="123" href="">Add Hash</a><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><script>$(document).ready(function() {    $("a").click(function(e) {        window.location.hash = $(this).attr("id");        e.preventDefault();    });});</script></body></html>


If you don't want jump the page then there is a workaround by using the history API on modern browsers with fallback on old ones:

if(history.pushState) {    history.pushState(null, null, '#myhash');} else {    location.hash = '#myhash';}

Credit goes to Lea Verou