Javascript Ajax-don';不要更改URL
我使用Ajax更改了页面的内容,但问题是站点url保持不变,因此根本不加载页面,只加载页面上的文本。例如,我点击了登录链接,内容发生了变化,但url保持在site/,而不是site/Login。实际的登录表单不会加载,因为它甚至不调用它,只加载基本文本。我怎样才能解决这个问题? 请注意,在网站上使用Zend 脚本:Javascript Ajax-don';不要更改URL,javascript,php,jquery,ajax,zend-framework2,Javascript,Php,Jquery,Ajax,Zend Framework2,我使用Ajax更改了页面的内容,但问题是站点url保持不变,因此根本不加载页面,只加载页面上的文本。例如,我点击了登录链接,内容发生了变化,但url保持在site/,而不是site/Login。实际的登录表单不会加载,因为它甚至不调用它,只加载基本文本。我怎样才能解决这个问题? 请注意,在网站上使用Zend 脚本: Ajax不会重新加载页面或加载其他页面,因此在发出Ajax请求时url不会更改 例如,如果您希望更改url,以便可以共享和标记ajax页面,则需要手动更改url 您可以使用html5
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应该更改为链接的url,但是没有真正遵循链接,而是发出了ajax请求
请注意,您还需要确保正确加载所有URL。这只是一个简单的示例,从外观上看,链接的url不会加载完整的页面
查看示例了解更多信息。这就是AJAX的本质。你以为会发生什么事?还有,为什么这是为PHP标记的?如果你想更改url,那么你必须执行window.location或其他操作,强制浏览器获取新的url…@Twisty我希望加载url的内容,而不重新提交布局,所以你只需要加载链接后的文本?你现在得到了什么?可以创建一个函数来去除标记。
// 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);
}
});