JQuery Ajax adding hash in the url

KyelJmD picture KyelJmD · Dec 21, 2012 · Viewed 43k times · Source

I have this code here that uses struts2-jquery plugin

<h4>Choose A task</h4>
    <ul>
        <s:url value="views/ajaxvalidation.jsp" var="ajaxvalidation" >
            <s:param name="menuId" value="1"/>
        </s:url>
        <li><sj:a targets="resultContent" href="%{ajaxvalidation}">Ajax Validation</sj:a></li>
    </ul>

When I click its content the url is changing to something like this, nothing is changing in the url. it still remains the same, what I want is that when I click the link something like this would happen www.myapp.com/#ajaxvalidation . When I run the code anchor tag is translated to something like this

<a id="anchor_1365013162" href="javascript:void(0)">Ajax Validation</a>

With that given, how would I add a hash in the url?

Answer

Moritz Petersen picture Moritz Petersen · Dec 21, 2012

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>