在JavaScript中,实现页面路径跳转主要有以下几种方法:
一、使用window.location
对象
window.location.href
window.location.href
的值为目标URL,就可以让浏览器导航到该URL。window.location.assign()
window.location.href
类似,它会加载新的文档。window.location.href
类似,在需要明确表示是进行页面分配加载的情况下使用。window.location.replace()
assign()
不同的是,不会在浏览器的历史记录中留下当前页面的记录,用户不能通过后退按钮返回到原页面。二、使用history
对象
history.pushState()
history.replaceState()
pushState()
类似,但是它会替换当前的历史记录条目而不是添加新的。如果遇到跳转失败的情况可能有以下原因:
领取专属 10元无门槛券
手把手带您无忧上云