如何使用 JavaScript 中的 window.location 重定向到不同的 URL?
javascriptweb developmentfront end technology更新于 2026/2/6 22:36:17
您可能遇到过这样的情况:您单击某个 URL 到达页面 X,但在内部却被定向到另一个页面 Y。这是由于页面重定向而发生的。
在客户端使用 JavaScript 进行页面重定向非常简单。要将网站访问者重定向到新页面,您只需要在 head 部分添加一行,如下所示 −
示例
您可以尝试运行以下代码来了解如何使用 window.location 重定向到另一个 URL。在这里,我们将重定向到主页
<html>
<head>
<script>
<!--
function Redirect() {
window.location.assign("https://www.tutorialspoint.com");
}
//-->
</script>
</head>
<body>
<p>单击以下按钮,您将被重定向到主页。</p>
<input type = "button" value="Redirect Me" onclick="Redirect();" />
</body>
</html>
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

