模仿Gitee实现站外链接跳转时进行确认
温馨提示:这篇文章已超过378天没有更新,请注意相关的内容是否还可用!
概述
如Gitee等网站,在有外部链接的时候如果不是同域则会出现一个确认页面。本文就带你看看这个功能应该如何实现。
效果
实现
1. 实现思路
将打开链接作为参数传递给一个中间页面,在页面加载的时候判断链接的域名和当前网站是否同域,同域则直接跳转,如果不同域,则展示确认页面进行在此确认。如下为跳转页面。
http://localhost:3001/#/link?target=https://www.baidu.com
2. 实现代码
即将跳转到外部网站
您将要访问的链接不属于{{ local }},请关注您的账号安全。
{{ target }}
继续前往
export default {
computed: {
isSameDomain() {
const host = this.local
const targetHost = new URL(this.target).host
return host === targetHost
},
target() {
return this.$route.query.target
},
local() {
return window.location.host
}
},
created() {
if(this.isSameDomain) window.location.href = this.target
},
methods: {
goto() {
window.location.href = this.target
}
}
}
$size: 38rem;
.layout-main {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
font-size: 1.2rem;
background: #efefef;
padding: 0 calc(50% - $size / 2);
h3 {
width: $size;
text-align: center;
font-size: 1.8rem;
margin: 0.5rem 0;
}
.layout-box {
background-color: #ffffff;
width: $size;
border-radius: 0.4rem;
box-shadow: 0.1rem 0.1rem 0.4rem #ccc;
padding: 1.5rem;
p {
margin: 0;
line-height:2;
&:first-child {
font-size: 1.5rem;
margin-bottom: 1rem;
line-height: 1;
}
&:last-child {
text-align: right;
}
}
}
}
``
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

