js怎么跳转到新页面?


本文摘自PHP中文网,作者青灯夜游,侵删。

js跳转到新页面的方法:1、使用location.href="新页面URL"来跳转;2、使用location.replace("新页面URL")来跳转;3、使用location.assign("新页面URL")来跳转。

方法1:使用location.href

语法:

1

location.href="URL"

示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

    </head>

    <body>

      <p>这是<i>location.href</i>方式的示例</p>

      <button onclick="myFunc()">点击这里</button>

         

      <!--重定向到其他网页的脚本-->  

      <script>

         function myFunc() {

           window.location.href="https://www.php.cn";

         }

      </script>

   </body>

</html>

1.gif

方法2:使用location.replace()

语法:

1

location.replace("URL")

示例:使用location.replace()方法跳转到其他网页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

    </head>

    <body>

      <p>这是<i>location.replace()</i>方式的示例</p>

      <button onclick="myFunc()">点击这里</button>

         

      <!--重定向到其他网页的脚本-->  

      <script>

         function myFunc() {

          location.replace("https://www.php.cn");

         }

      </script>

   </body>

</html>

2.gif

方法3:使用location.assign()

语法:

1

location.assign("URL")

示例:使用location.assign()方法跳转到其他网页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

    </head>

    <body>

      <p>这是<i>location.assign()</i>方式的示例</p>

      <button onclick="myFunc()">点击这里</button>

         

      <!--重定向到其他网页的脚本-->  

      <script>

         function myFunc() {

          location.assign("https://www.php.cn");

         }

      </script>

   </body>

</html>

3.gif

推荐教程:《JavaScript视频教程》

以上就是js怎么跳转到新页面?的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

js proxy 的优势以及使用场景

一分钟了解js中int和string的转换(附代码)

js实现统计字符出现的次数并去重的方法

js如何实现原生map

浅谈js中的bind()

js怎么补齐数字

网页代码中js和css指的是什么

javascript怎么读取map的值

js干货分享―-this指向问题

javascript时间戳如何转化为日期

更多相关阅读请进入《js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...