JavaScript实现页面跳转的多种方法详解
本文汇总了JavaScript中实现页面跳转的多种常用方式,包括在当前窗口跳转、新窗口打开、返回上一页以及通过Meta标签定时刷新等场景。详细解析了window.location、window.open及history对象的具体用法与区别,并提供带参跳转、按钮事件绑定及iframe框架内跳转的实际代码示例,帮助开发者根据需求选择最合适的跳转方案。
JavaScript页面跳转方法概述
汇总JavaScript实现网页链接跳转的多种方法
当前窗口与链接跳转实现
1、在当前窗口中打开链接
window.location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow"
// 等价于
<a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_self">跳转到百度</a>新窗口打开与返回上一页
2、在新建窗口中打开链接
window.open("http://www.baidu.com")
// 等价于
<a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank">跳转到百度</a>3、返回至上一页
window.history.back(-1)复杂场景下的跳转处理
4、执行链接跳转
self.location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" self代表当前窗口对象,它是window层级中的顶层对象。
location.href // 指的是某window对象的url的地址
self.location.href // 指当前窗口的url地址,去掉self默认为当前窗口的url地址,一般用于防止外部的引用
top.location.href // 为引用test.html页面url的父窗口对象的url假设你的网页地址为http://www.a.com,而对方网站为http://www.b.com,若对方通过iframe框架引用你的页面,你可以使用以下代码:
if(top.location.href!=self.location.href){
location.href="http://www.a.com" rel="external nofollow" ;
}以转向你的页面,top代表主体窗口,此时top.location.href返回的是http://www.b.com;
以下简要总结了实现页面跳转功能的几种方法
具体跳转场景代码示例
第一种:利用JavaScript进行页面跳转
1)带有参数的跳转
<script language="javascript" type="text/javascript">
window.location.href="http://www.a.com?backurl=" rel="external nofollow" + 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)" rel="external nofollow" >返回上一步</a>
<a href="<%=Request.ServerVariables(" rel="external nofollow" HTTP_REFERER")%>">返回上一步</a>第三种:为button按钮添加事件实现跳转
<input name="前端知音" type="button" value="前端知音" onClick="location.href='login.do'">
第四种:在新窗口中打开
<a href="javascript:" rel="external nofollow" 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>至此,关于JS实现页面跳转链接的几种方式的文章介绍完毕,更多相关js页面跳转链接内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!
JavaScript异步发送HTTP请求时怎么优雅地处理错误
2026-11-05 17:10:44
5款主流JS在线运行工具对比:CodePen、JSFiddle与StackBlitz怎么选
2026-11-05 17:04:04
JavaScript入门书籍避坑:这些书不适合初学者
2026-11-05 16:31:15
JS使用Math.round实现保留两位小数的原理与代码
2026-11-05 15:35:55
JavaScript 原生 Date 对象怎么实现日期加减
2026-11-05 14:30:00
相关标签:
