Several methods of JS to realize page jump

Several methods of JS to realize page jump

To achieve jumping from one page A to another page B, the js implementation is to add the jump code to the js code of A

JS jump probably has the following ways:

The first type: (jump to b.html)

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

The second type: (return to the previous page)

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

The third type:

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

The fourth type:

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

The fifth type:

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

Page pass value:

first page

<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>

second page

<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>

Note: Chinese transmission: you can use encodeURI to encode url on page a and decode url with decodeURI on page b

Summary of several ways to use JS to realize page jump

The first method: use JS to jump to the page

1) Jump with parameters

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

2) Jump without parameters

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

The second method: return to the previous preview interface

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

HTML page nesting

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

The third type: button button to add event jump

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

Fourth: open in a new window

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

The fifth type: set the jump page through meta

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

Reference article:

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

https://blog.csdn.net/afreon/article/details/119617930

https://blog.csdn.net/yiye2017zhangmu/article/details/82782374

Guess you like

Origin blog.csdn.net/qq_53729147/article/details/127788211