【问题标题】:how to redirect on specific page using javascript如何使用javascript在特定页面上重定向
【发布时间】:2021-08-27 02:46:27
【问题描述】:

我在使用 javascript 单击按钮时无法从一个页面重定向到另一个页面?

比如说,一个页面是index.php,另一个是sub-page.php,我如何使用javascript从主页重定向到子页面?谁能指导我?文件位于我项目的根目录中。

我已经尝试过以下代码,但它不起作用:

HTML 代码:

<button type="button" onclick="test()">click me</button>

js代码:

function test(){
 window.location.href = "./sub-page.php";
}

【问题讨论】:

  • 请比“不工作”更具描述性。实际发生了什么? sub-page.php 是位于文档根目录下还是某个子文件夹下?
  • 可能是子page.php的路径不对。你也可以使用window.open("sub-page.php", "_self");
  • 为什么你会想要滥用一个按钮,显然链接的工作是什么?

标签: javascript php redirect location


【解决方案1】:

你可以用这个。

<script>window.location.href = "/sub-page.php";</script>

<script>window.location.href = "http://www.google.com";</script>

【讨论】:

    【解决方案2】:

    window.location.href = "子页面.php";

    【讨论】:

      【解决方案3】:

      首先检查sub-page的正确路径你也可以使用window.open。

      function test(){
          window.open("sub-page.php", "_self")
      }
      

      【讨论】:

        【解决方案4】:

        浏览器的控制台有错误吗?

        如果没有,您可以在函数中添加一些“console.log”消息,例如

        function test(){
         console.log("before href");
         window.location.href = "./sub-page.php";
         console.log("after href");
        }
        

        这样,您可以通过查看控制台输出来判断您的函数是否被正确调用。

        始终尝试至少找到一条错误消息,而不是仅仅说它不起作用。仅凭“不工作”,我们将无法为您提供太多帮助。

        【讨论】:

          猜你喜欢
          • 2012-08-16
          • 1970-01-01
          • 2021-09-17
          • 1970-01-01
          • 2022-06-18
          • 2021-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多