2

我创建了一个大量使用 Ajax 加载其内容的网站。我希望我选择的页面在 AJAX 上加载,但同时 URL 也会更改,而无需重新加载整个页面内容。我将如何实现这一目标?我已经用谷歌搜索了,但我的搜索没有产生任何结果。

假设我有这个页面:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <sj:head />
</head>
<body>
    <h5>Struts Jquery Ajax Integration </h5>

    <div id="resultContent"></div>
    <noscript>Please Turn On Javascript to make the full use of this site</noscript>

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

    <div>
        <h6>Play A Music while You Navigate</h6>
        <audio src="x.mp3" controls>Your browser does not support the
            audio element.
        </audio>
    </div>
</body>
</html>

我单击了 Ajax 验证链接。它不会重新加载页面,但是 url 会是这样的:

localhost:8090/AppName/ajaxvalidation.jsp

或这个:

 localhost:8090/AppName/ajaxvalidation.action

我将如何实现这样的目标?

请注意,我使用的是这个插件:struts2-jquery

4

2 回答 2

3

您需要使用History API

window.history.pushState(data, title, url)

更新

请注意,struts2-jquery您可以添加ajaxhistory="true"以启用构建 ajax 历史记录功能。请参阅http://code.google.com/p/struts2-jquery/wiki/HeadTag#Attributes

<sj:head ajaxhistory="true" />

但请记住,并非所有浏览器版本都支持它。

于 2012-12-20T18:25:43.617 回答
0

history.pushState如果浏览器支持(阅读 < IE10) ,您可以使用

var boolPushState  = false;
if (typeof history.pushState !== "undefined") {
        boolPushState = true;
}

if (boolPushState) {
    history.pushState(null, null, "URL");
}
于 2012-12-20T18:26:58.870 回答