JS 实现页面跳转的几种方法

JS页面跳转方法总结
本文介绍了使用JavaScript实现页面跳转的多种方法,包括直接跳转、带参数跳转、返回上一页、按钮触发跳转及新窗口打开等,并演示了页面间传值的具体实现。

JS 实现页面跳转的几种方法

要实现从一个页面A跳到另一个页面B,js实现就在A的js代码加跳转代码

JS跳转大概有以下几种方式:

第一种:(跳转到b.html)

<script language="javascript" type="text/javascript">
   window.location.href="b.html";
 </script>

第二种:(返回上一页面)

<script language="javascript">
   window.history.go(-1);
 </script>

第三种:

<script language="javascript">
   window.navigate("b.html");
 </script>

第四种:

<script language="JavaScript">
   self.location=’b.html’;
 </script>

第五种:

<script language="javascript">
   top.location=’b.html’;
 </script>

页面传值:

第一个页面

<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>无标题文档</title>
 </head>
 <SCRIPT LANGUAGE="JavaScript">
     function test(){
         var s = document.getElementById("txt");
         location.href="test2.html?"+"txt="+encodeURI(s.value);
     }
 </SCRIPT>
 <body>
  
 <input type="text" id="txt">
 </br>
 <input type="button" value="TEST" onclick="test()"/>
  
 </body>
 </html>

第二个页面

<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>无标题文档</title>
 </head>
  
 <body>
   <script language="javascript" type="text/javascript">
   var loc = location.href;
   var n1 = loc.length;//地址的总长度
   var n2 = loc.indexOf("=");//取得=号的位置
   var id = decodeURI(loc.substr(n2+1, n1-n2));//从=号后面的内容
   alert(id);
   //document.write(id)
   </script>
 </body>
 </html>

注:中文传输:可以在页面a用encodeURI 编码url 在b页面用decodeURI解码url

使用 JS 实现页面跳转的几种方式总结

第一种:使用JS跳转页面

1)跳转带参

<script language="javascript" type="text/javascript">
        window.location.href="jingxuan.do?backurl=" + window.location.href; 
 </script>

2)跳转无参

<script>window.location.href='http://blog.yoodb.com';</script>

第二种:返回上一次预览界面

<script language="javascript">
   alert("返回");
   window.history.back(-1);
 </script>

HTML页面嵌套

<a href="javascript:history.go(-1)">返回上一步</a>
 <a href="<%=Request.ServerVariables("HTTP_REFERER")%>">返回上一步</a>

第三种:button按钮添加事件跳转

<input name="前端知音" type="button" value="前端知音" onClick="location.href='login.do'">

第四种:在新窗口打开

<a href="javascript:" onClick="window.open('login.do','','height=500,width=611,scrollbars=yes,status=yes')">新窗口</a>

第五种:通过meta设置跳转页面

<head>
     <!--只刷新不跳转 -->
     <meta http-equiv="refresh" content="5">
     <!--定时跳转 -->
     <meta http-equiv="refresh" content="5;url=index.html"> 
 </head>

参考文章:

https://www.cnblogs.com/lyggqm/p/5688028.html

https://blog.youkuaiyun.com/afreon/article/details/119617930

https://blog.youkuaiyun.com/yiye2017zhangmu/article/details/82782374

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值