如何使用 window.location 通过 JavaScript 重定向到不同的 URL?


你可能遇到过这样的情况:点击一个 URL 访问页面 X 时,你却被内部定向到了另一个页面 Y。这是由于页面重定向造成的。

在客户端使用 JavaScript 进行页面重定向非常简单。只需在你的 head 部分添加以下一行,即可将你的网站访问者重定向到新页面 −

示例

你可以尝试运行以下代码,了解如何使用 window.location 重定向到另一个 URL。在此,我们将重定向到主页

实际演示

<html>
   <head>
      <script>
         <!--
            function Redirect() {
               window.location.assign("https://tutorialspoint.com");
            }
         //-->
      </script>
   </head>
   <body>
      <p>Click the following button, you will be redirected to home page.</p>
      <input type = "button" value="Redirect Me" onclick="Redirect();" />
   </body>
</html>

更新于: 2020 年 6 月 12 日

413 次浏览

启动你的 职业

通过完成课程进行认证

开始
广告
© . All rights reserved.