使用window.location.href:
window.location.href = "https://www.example.com";
这会将页面重定向到指定的URL。
使用window.location.replace:
window.location.replace("https://www.example.com");
这也会将页面重定向到指定的URL,但与window.location.href不同的是,它不会在浏览器的历史记录中创建新的记录,因此用户不能通过返回按钮返回上一页。
使用window.location.assign:
window.location.assign("https://www.example.com");
这也会将页面重定向到指定的URL,并在浏览器的历史记录中创建一个新的记录,允许用户通过返回按钮返回上一页。
使用window.location.replace 或 window.location.assign 时,你也可以使用相对路径来跳转到当前域内的其他页面,例如:
window.location.replace("/subdirectory/page.html");
或者
window.location.assign("/subdirectory/page.html");
使用window.open:
window.open("https://www.example.com", "_blank");
这将在一个新的浏览器窗口或标签页中打开指定的URL。
使用超链接:
你也可以在HTML中使用超链接来实现页面跳转:
<a href="https://www.example.com">Click here to go to Example</a>
或者使用相对路径:
<a href="/subdirectory/page.html">Click here to go to a page in the same dom</a>