惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

博客园 - 逸乐太子

豆包无水印图片下载器|新媒体运营、自媒体创作者必备神器 免费无广告!网易UU远程,解锁远程办公、娱乐、运维全能玩法 告别手动复制!公众号文章批量导出工具,极致提升内容运营效率 高效办公神器:可视化Excel文档合并工具,告别手动复制粘贴 全国分省 Excel 爬取后,一键统计全文件数据行数|附即用型统计工具 某速体育网站中canvas数据获取 台风数据采集全攻略:从数据源到实操落地 豆包生成的图片带有AI字样怎么去除? 中国马拉松官方网站赛历信息采集工具 国家标准批量采集工具:全量元数据检索 + 批量导出 知网文献批量采集:BibTeX/EndNote 完整导出(含摘要、关键词、被引、全文链接) 超级课堂题库下载本地工具 洋葱智课在线题库导出工具 【python爬虫定制】PubScholar公益学术平台期刊名称爬取 【python数据采集】阳光高考学校信息采集 python程序的守护工具 【Python数据采集】国家自然科学基金大数据知识管理服务门户数据采集 pyinstaller 打包时第三方模块与图片资源加载 使用python爬取豆瓣电影短评评论内容 使用 Python 爬取高校教师信息 python从PDF文件中读取国民经济行业分类 python爬虫之JS逆向某易云音乐 国务院办公厅放假通知信息获取 python爬虫之JS逆向 python爬虫之企某科技JS逆向 linux下telnet命令有时无法退出 H5中腾讯地图定位信息在安卓手机中获取不到
使用 js 实现 urljoin 方法
逸乐太子 · 2023-03-15 · via 博客园 - 逸乐太子

需求:

在 js 中将两个网址信息拼接在一起。

如下代码如下:

String.prototype.endsWith = function (end) {
    return this.length > 0 && this.indexOf(end) + end.length == this.length;
  };
  String.prototype.rfind = function (c) {
    for (let i = this.length - 1; i >= 0; i--) {
      if (this.split("")[i] == c) {
        return i;
      }
    }
    return -1;
  };
  window.urljoin = function (url_base, relative_url) {
    let length = relative_url.length,
      relative_url2 = relative_url.replace(/\.\.\//g, ""),
      length2 = relative_url2.length,
      level_len = (length - length2) / 3;

    if (relative_url.indexOf("./") == 0) {
      return (
        url_base.substring(0, url_base.rfind("/") + 1) +
        relative_url.substring(2)
      );
    } else if (relative_url.indexOf("../") == 0) {
      let count = 0;
      for (let i = 0; i < url_base.length; i++) {
        let s = url_base[url_base.length - 1 - i];
        if (s == "/") {
          count++;
        }
        if (count == level_len + 1) {
          return url_base.substring(0, url_base.length - i) + relative_url2;
          break;
        }
      }
    } else if (relative_url.indexOf("/") == 0) {
      return new URL(url_base).origin + relative_url;
    }else if(relative_url.indexOf('http://') == 0 || relative_url.indexOf('https://') == 0){
        return relative_url
    } else {
      return (
        url_base.substring(0, url_base.rfind("/") + 1) +
        relative_url
      );
    }
  };

posted @ 2023-03-15 23:16  逸乐太子  阅读(172)  评论()    收藏  举报