xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

How to use js to parse a url string to a url object All In One

How to use js to parse a url string to a url object All In One

如何利用js将url字符串解析为url对象

image

try

// const url = globalThis.window.location.href;
// const url = window.location.href;

const autoConvertPageToRepo = (page = ``) => {
  if(!globalThis.window) {
    throw new Error(`❌ 当前的 js 运行环境不支持 Web API!`)
  }
  let url = ``;
  try {
    if(!page) {
      // in io page ✅
      const host = window.location.host.split(`.`)[0];  
      const protocol = window.location.protocol;
      const pathname = window.location.pathname;
      url = `${protocol}//github.com/${host}${pathname}`;
    } else {
      // URL parser,ulr string => URL
    }
    console.log(`✅ repo url =`, url)
  } catch (error) {
    console.error(`❌ generate repo url error`, error)
  }
  return url;
}


// test cases
autoConvertPageToRepo();
// autoConvertPageToRepo(`https://txstc55.github.io/ugly-avatar/`);
// https://github.com/txstc55/ugly-avatar/

image

solution ✅

const autoConvertPageToRepo = (uri = ``) => {
  if(!globalThis.window) {
    throw new Error(`❌ 当前的 js 运行环境不支持 Web API!`)
  }
  let url = ``;
  try {
    if(!uri) {
      // in io page ✅
      const host = window.location.host.split(`.`)[0];  
      const protocol = window.location.protocol;
      const pathname = window.location.pathname;
      url = `${protocol}//github.com/${host}${pathname}`;
    } else {
      // URL parser,ulr string => URL 🚀
      const location = new URL(uri);
      const host = location.host.split(`.`)[0];  
      const protocol = location.protocol;
      const pathname = location.pathname;
      url = `${protocol}//github.com/${host}${pathname}`;
    }
    console.log(`✅ repo url =`, url)
  } catch (error) {
    console.error(`❌ generate repo url error`, error)
  }
  return url;
}


// test cases
autoConvertPageToRepo();
// autoConvertPageToRepo(`https://txstc55.github.io/ugly-avatar/`);
// ✅ repo url = https://github.com/txstc55/ugly-avatar/

demos

use js auto convert GitHub Page URL To GitHub Repo URL 🚀

autoConvertPageToRepo();
// ✅ repo url = https://github.com/txstc55/ugly-avatar/

autoConvertPageToRepo(`https://txstc55.github.io/ugly-avatar/`);
// ✅ repo url = https://github.com/txstc55/ugly-avatar/

image

https://cdn.xgqfrms.xyz/github/github-page-to-github-repo.js

URL API

// window.location
const location = new URL(uri);

https://developer.mozilla.org/en-US/docs/Web/API/URL_API

https://developer.mozilla.org/en-US/docs/Web/API/URL

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

https://developer.mozilla.org/en-US/docs/Web/API/URL/parse_static

refs

https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript

https://dmitripavlutin.com/parse-url-javascript/



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(5)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-10-30 git Rewriting History All In One
2020-10-30 手把手教你使用 js 实现一个 Canvas 编辑器 All In One
2020-10-30 Next.js 10
2020-10-30 Swift 5.3 All In One
2020-10-30 React Hooks: useImperativeHandle All In One
2020-10-30 React Hooks: useDebugValue All In One
2020-10-30 React Hooks: useMemo All In One
点击右上角即可分享
微信分享提示