【发布时间】:2011-04-09 19:16:03
【问题描述】:
我正在使用 ajax 加载我的网站内容,并希望在 ajax 成功时更新窗口位置。
如何将窗口位置更新为“/newpage”?我需要用户能够返回并刷新。这可能吗??
【问题讨论】:
标签: javascript jquery ajax window location
我正在使用 ajax 加载我的网站内容,并希望在 ajax 成功时更新窗口位置。
如何将窗口位置更新为“/newpage”?我需要用户能够返回并刷新。这可能吗??
【问题讨论】:
标签: javascript jquery ajax window location
我假设您正在使用 jquery 进行 AJAX 调用,因此您可以通过将重定向置于成功中来轻松完成此操作,如下所示:
$.ajax({
url: 'ajax_location.html',
success: function(data) {
//this is the redirect
document.location.href='/newpage/';
}
});
【讨论】:
您可以为此设置document.location.href 的值。它指向当前的 URL。不需要 jQuery 来执行此操作。
【讨论】:
#hash 部分(如果您使用它)。
您可以在history manipulation API 中使用新的推送/弹出状态功能。
【讨论】:
假设您想将 url 更改为同一域中的另一个,您可以使用:
history.pushState('data', '', 'http://www.yourcurrentdomain.com/new/path');
【讨论】:
如果您想使用后退按钮,请查看此按钮。 https://stackoverflow.com/questions/116446/what-is-the-best-back-button-jquery-plugin
使用 document.location.href 更改页面位置,将其放置在成功运行 ajax 的函数中。
【讨论】:
我正在编写更改窗口的常用函数
此代码可在所有类型的项目中并行使用
function changewindow(url,userdata){
$.ajax({
type: "POST",
url: url,
data: userdata,
dataType: "html",
success: function(html){
$("#bodycontent").html(html);
},
error: function(html){
alert(html);
}
});
}
【讨论】: