在JavaScript中,我们可以使用location.replace()
或location.href
来重定向页面。 通常,我们使用location.replace()
模拟HTTP重定向。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>JavaScript - How to redirect a page</h1>
<h3>1. location.replace</h3>
<button onclick="httpRedirect()">location.replace</button>
<h3>2. location.href</h3>
<button onclick="mouseClick()">location.href</button>
<script>
// Simulate an HTTP redirect
function httpRedirect() {
location.replace("https://www.mkyong.com")
}
// Simulate a mouse click
function mouseClick() {
location.href("https://www.yahoo.com")
}
</script>
</body>
</html>
参考文献
标签: JavaScript 重定向
翻译自: https://mkyong.com/javascript/javascript-how-to-redirect-a-page/