Javascript 如何使用history.pushState更改url以使用jQuery Ajax创建页面转换?
我正在尝试使用window.history.pushState()实现无缝页面转换,以更改地址栏中的url。我遵循了此操作,但pushState引发了以下错误:Javascript 如何使用history.pushState更改url以使用jQuery Ajax创建页面转换?,javascript,jquery,transition,Javascript,Jquery,Transition,我正在尝试使用window.history.pushState()实现无缝页面转换,以更改地址栏中的url。我遵循了此操作,但pushState引发了以下错误:Uncaught DomeException:未能对“History”执行“pushState”:无法在源代码为“null”且URL为“code”的文档中创建URL为[link]的历史状态对象 有人知道如何解决这个问题吗。我试图实现的是,获取所单击按钮的href并更改url,例如:http://www.example.com/index.
Uncaught DomeException:未能对“History”执行“pushState”:无法在源代码为“null”且URL为“code”的文档中创建URL为[link]的历史状态对象
有人知道如何解决这个问题吗。我试图实现的是,获取所单击按钮的href并更改url,例如:http://www.example.com/index.html
至http://www.example.com/about.html
$('.button').on('click', function(e) {
e.preventDefault();
var href = $(this).attr('href');
window.history.pushState(null, null, href);
$.ajax({
url: href,
success: function(data) {
// Do animation and change content
}
});
});
如果您试图从http://www.example.com/index.html
$('.button').on('click', function(e) {
e.preventDefault();
var href = $(this).attr('href');
window.history.pushState(null, null, href);
$.ajax({
url: href,
success: function(data) {
// Do animation and change content
}
});
});
null
origin是一个URL以file:
开头的页面
您需要在一个原点不是null
的页面中运行JS。i、 e.在具有HTTP(S)URL的web服务器上