js实现页面跳转链接的几种方式

js实现页面跳转链接的几种方式

1、在当前窗口打开
window.location.href="http://www.baidu.com" 
// 等价于    
<a href="http://www.baidu.com" target="_self">跳转到百度</a>
2、在新窗口打开
window.open("http://www.baidu.com")  
// 等价于 
<a href="http://www.baidu.com" target="_blank">跳转到百度</a>
3、返回上一页
window.history.back(-1)
4、跳转链接
self.location.href="http://www.baidu.com"

self 指代当前窗口对象,属于window 最上层的对象。

location.href   // 指的是某window对象的url的地址

self.location.href   // 指当前窗口的url地址,去掉self默认为当前窗口的url地址,一般用于防止外部的引用

top.location.href   // 为引用test.html页面url的父窗口对象的url
if(top.location.href!=self.location.href){

       location.href="http://www.a.com";

}

来转向你的页面,top指代的是主体窗口,这里top.location.href返回http://www.b.com;

下面简单总结了几种页面跳转功能的实现

第一种:使用JS跳转页面

1)跳转带参

<script language="javascript" type="text/javascript">
       window.location.href="http://www.a.com?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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值