更改窗口位置Jquery
我正在使用ajax加载我的网站内容,并希望在ajax成功时更新窗口位置。
如何将窗口位置更新为“/ newpage”? 我需要用户能够返回并刷新。 这可能吗??
我假设您正在使用jquery进行AJAX调用,因此您可以通过将重定向放入成功中来轻松完成此操作,如下所示:
$.ajax({ url: 'ajax_location.html', success: function(data) { //this is the redirect document.location.href='/newpage/'; } });
您可以为此目的设置document.location.href
的值。 它指向当前的URL。 jQuery不需要这样做。
您可以在历史记录操作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
此代码可以在所有类型的项目中并行使用
function changewindow(url,userdata){ $.ajax({ type: "POST", url: url, data: userdata, dataType: "html", success: function(html){ $("#bodycontent").html(html); }, error: function(html){ alert(html); } }); }