Ajax - 不更改网址
Ajax - doesn't change URLs
我使用 Ajax 更改了页面内容,但问题是网站 url 保持不变,因此根本不会加载页面,只会加载它。因此,例如,我单击登录 link,内容发生变化,但 url 保留在站点/,而不是 site/login。实际的登录表单不会加载,因为它甚至没有调用它,只加载基本文本。我该如何解决?
P.S。在网站上使用 Zend
脚本:
$(document).ready(function() {
$('a').click(function() {
var toLoad = $(this).attr('href');
$('#content').load(toLoad);
return false;
});
});
Ajax 不会重新加载页面或加载另一个页面,因此当您发出 ajax 请求时 url 不会更改。
如果您想要更改 url,例如您的 ajax 填充的页面可以共享和添加书签,您需要手动更改 url。
您可以使用 html5 历史记录 API。
一个简单的例子:
// we need the click event here
$('a').click(function(e) {
// cancel default click action using `e`
e.preventDefault();
var toLoad = $(this).attr('href');
$('#content').load(toLoad);
// check if the html5 history api is available in the browser first
if (window.history && window.history.pushState) {
// push the state to the url in the address bar
history.pushState({}, e.target.textContent, e.target.href);
}
});
现在地址栏中的 url 应该变成 link 的 url 但是 link 并没有真正跟随,而是 ajax 已提出请求。
请注意,您还需要确保所有 url 都正确加载。这只是一个简单的示例,从外观上看,您的 linked url 不会加载完整的页面。
查看 documentation on mozilla.org 以获取更多信息。
我使用 Ajax 更改了页面内容,但问题是网站 url 保持不变,因此根本不会加载页面,只会加载它。因此,例如,我单击登录 link,内容发生变化,但 url 保留在站点/,而不是 site/login。实际的登录表单不会加载,因为它甚至没有调用它,只加载基本文本。我该如何解决? P.S。在网站上使用 Zend 脚本:
$(document).ready(function() {
$('a').click(function() {
var toLoad = $(this).attr('href');
$('#content').load(toLoad);
return false;
});
});
Ajax 不会重新加载页面或加载另一个页面,因此当您发出 ajax 请求时 url 不会更改。
如果您想要更改 url,例如您的 ajax 填充的页面可以共享和添加书签,您需要手动更改 url。
您可以使用 html5 历史记录 API。
一个简单的例子:
// we need the click event here
$('a').click(function(e) {
// cancel default click action using `e`
e.preventDefault();
var toLoad = $(this).attr('href');
$('#content').load(toLoad);
// check if the html5 history api is available in the browser first
if (window.history && window.history.pushState) {
// push the state to the url in the address bar
history.pushState({}, e.target.textContent, e.target.href);
}
});
现在地址栏中的 url 应该变成 link 的 url 但是 link 并没有真正跟随,而是 ajax 已提出请求。
请注意,您还需要确保所有 url 都正确加载。这只是一个简单的示例,从外观上看,您的 linked url 不会加载完整的页面。
查看 documentation on mozilla.org 以获取更多信息。