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

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

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.csdn.net/afreon/article/details/119617930

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

相关文章
|
5天前
|
JSON JavaScript 前端开发
JavaScript实现字符串转json对象的方法
JavaScript实现字符串转json对象的方法
|
1天前
|
JavaScript 数据安全/隐私保护
如何在Vue组件中调用封装好的外部js文件方法
这篇文章介绍了如何在Vue组件中调用封装好的外部js文件方法,包括在Vue项目中全局引入外部js文件,并在组件中通过this.$myMethod()的方式调用外部js文件中定义的方法。
如何在Vue组件中调用封装好的外部js文件方法
|
4天前
|
JavaScript 测试技术 索引
js数组方法汇总
js数组方法汇总
7 1
|
5天前
|
JavaScript 前端开发
JavaScript 地址信息与页面跳转及跳转传参
JavaScript 地址信息与页面跳转及跳转传参
13 1
|
5天前
|
JavaScript 前端开发 UED
JavaScript 地址信息与页面跳转
JavaScript 地址信息与页面跳转
|
1天前
|
JavaScript Linux
2022年超详细在CentOS 7上安装Node.js方法(源码安装)
这篇文章介绍了在CentOS 7系统上通过源码安装Node.js的详细步骤,包括从官网下载Node.js源码包、将安装包上传至虚拟机、解压安装包、删除压缩文件、编译安装Node.js、检查Node.js和npm版本,以及切换npm源到淘宝镜像以加速下载。此外,还提供了一个获取Linux下Node.js离线安装包的微信公众号搜索方式。
|
5天前
|
JavaScript 前端开发
javascript中常见获取时间戳的方法
javascript中常见获取时间戳的方法
13 0
|
5天前
|
JavaScript 前端开发
js中this是指向的哪个全局变量,改变this指向的方法有什么?
js中this是指向的哪个全局变量,改变this指向的方法有什么?
7 0
|
5天前
|
JavaScript
js怎样控制浏览器前进、后退、页面跳转
js怎样控制浏览器前进、后退、页面跳转
12 0
|
5天前
|
前端开发 JavaScript
JavaScript 获取 HTML 元素方法
JavaScript 获取 HTML 元素方法
10 0