模仿Gitee实现站外链接跳转时进行确认

概述

如Gitee等网站,在有外部链接的时候如果不是同域则会出现一个确认页面。本文就带你看看这个功能应该如何实现。

效果

image.png

实现

1. 实现思路

将打开链接作为参数传递给一个中间页面,在页面加载的时候判断链接的域名和当前网站是否同域,同域则直接跳转,如果不同域,则展示确认页面进行在此确认。如下为跳转页面。

http://localhost:3001/#/link?target=https://www.baidu.com

2. 实现代码

<template>
    <div class="layout-main">
      <div class="layout-box">
        <p>即将跳转到外部网站</p>
        <p>您将要访问的链接不属于{{ local }},请关注您的账号安全。</p>
        <p>{{ target }}</p>
        <p>
          <el-button type="warning" @click="goto">继续前往</el-button>
        </p>
      </div>
    </div>
  </template>
  
  <script>
  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
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  $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;
        }
      }
    }
  }
  </style>
``


相关推荐

  1. HTML超(详解如何进行网页之间的

    2024-03-11 00:56:04       34 阅读
  2. uniapp内实现到浏览器网页上

    2024-03-11 00:56:04       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-11 00:56:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-11 00:56:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-11 00:56:04       82 阅读
  4. Python语言-面向对象

    2024-03-11 00:56:04       91 阅读

热门阅读

  1. [three.js]UV动画

    2024-03-11 00:56:04       44 阅读
  2. 【IC设计】Scala、Chisel、Chiseltest版本兼容信息

    2024-03-11 00:56:04       49 阅读
  3. iOS面试题

    2024-03-11 00:56:04       50 阅读
  4. 力扣题库第3题:最长连续序列

    2024-03-11 00:56:04       35 阅读
  5. c++之迭代器与反向迭代器

    2024-03-11 00:56:04       42 阅读
  6. 序列的第 k 个数(c++题解)

    2024-03-11 00:56:04       54 阅读
  7. OceanBase社区版单节点安装搭建(Docker)

    2024-03-11 00:56:04       41 阅读
  8. Hyperf AOP 和 注解

    2024-03-11 00:56:04       45 阅读
  9. mysql 8 修改账号密码

    2024-03-11 00:56:04       39 阅读
  10. 链表简单功能的总结

    2024-03-11 00:56:04       35 阅读
  11. Ubuntu设置时区和时间同步

    2024-03-11 00:56:04       53 阅读
  12. 【国产MCU】-窗口看门狗(WWDG)

    2024-03-11 00:56:04       46 阅读