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怎么判断是否为数字

js中substring、slice与substr的区别有哪些?

箭头函数的理解

jquery表单插件jquery.form.js

如何利用js对css样式进行更改

详解js中switch语句的使用方法

h5+js实现本地文件读取和写入

介绍js 表单提交信息加密

javascript中alert用法是什么

js中!与!!的用法介绍

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




打赏

取消

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

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

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

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

评论

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