我正在使用ajax加载我的网站内容,并希望在ajax成功时更新窗口位置。
如何将窗口位置更新为“/ newpage”?我需要用户能够返回并刷新。这可能吗?
答案 0 :(得分:43)
我假设您正在使用jquery进行AJAX调用,因此您可以通过将重定向放入成功中来轻松完成此操作:
$.ajax({
url: 'ajax_location.html',
success: function(data) {
//this is the redirect
document.location.href='/newpage/';
}
});
答案 1 :(得分:6)
您可以为此目的设置document.location.href
的值。它指向当前的URL。 jQuery不需要这样做。
答案 2 :(得分:4)
您可以使用history manipulation API中的新推/弹状态函数。
答案 3 :(得分:3)
假设您要将网址更改为同一域内的其他网址,可以使用以下命令:
history.pushState('data', '', 'http://www.yourcurrentdomain.com/new/path');
答案 4 :(得分:0)
如果您想使用后退按钮,请检查一下。 https://stackoverflow.com/questions/116446/what-is-the-best-back-button-jquery-plugin
使用document.location.href更改页面位置,将其置于成功运行ajax的函数中。
答案 5 :(得分:0)
我正在为改变窗口编写常用功能
此代码可以在所有类型的项目中并行使用
function changewindow(url,userdata){
$.ajax({
type: "POST",
url: url,
data: userdata,
dataType: "html",
success: function(html){
$("#bodycontent").html(html);
},
error: function(html){
alert(html);
}
});
}