1 Star 0 Fork 0

Yaohaixiao / parseURL.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

parseURL.js

npm version Github file size prettier code style Coverage npm downloads MIT License

parseURL.js - 专门解析 URL 地址信息的 JavaScript 工具。

项目初衷

编写 parseURL.js 主要是为初学 JavaScript 的朋友了解在 JavaScript 中如何使用 JavaScript 解析 URL?而开发的。当然,parseURL.js 也完全可以胜任在实际产品开发的应用。

Features

  • 原生 JavaScript 编写,无任何依赖;
  • 支持 UMD 模块规范,同时也提供 ES6 模块调用;
  • 解析的 URL 地址信息非常全面:
    1. href - 完整 URL 地址
    2. protocol - 协议
    3. username - 用户名
    4. password - 密码
    5. host - 域名地址
    6. hostname - 域名名称
    7. port - 端口号
    8. path - 路径
    9. pathname - 路径名
    10. search - 查询参数
    11. hash - 哈希值
    12. origin
    13. searchParams
  • 文件体积小(GZip:1KB),加载速度快;

Browsers support

IE / EdgeIE / Edge FirefoxFirefox ChromeChrome SafariSafari OperaOpera
IE11, Edge last 10 versions last 10 versions last 10 versions last 10 versions

Install

parseURL.js 支持 AMD 和 CommonJS 规范的模块调用方式,可以直接使用 npm 安装,也可以使用 script 标签引入到页面。

npm install

npm i -S @yaohaixiao/parseurl.js

script

可以根据项目的实际情况,选择调用 CDN 文件或者本地文件。

CDN

<script src="https://cdn.jsdelivr.net/gh/yaohaixiao/parseurl.js/parseURL.min.js"></script>

Local

<script src="path/to/parseURL.min.js"></script>

Usage

parseURL.js 支持 UMD 模块规范,也支持作为 ES6 模块使用。

作为 CommonJS 模块使用

// Node.js 环境中使用
const parseURL = require('@yaohaixiao/parseurl.js')

// 使用绝对路径
const url = parseURL('https://github.com/yaohaixiao/parseurl.js?id=23')

// 获取查询字符串中 id 的值,如果没有则返回空
url.searchParams.get('id') // -> '23'

作为 ES6 模块使用

// 作为 ES6 模块使用
import parseURL from '@yaohaixiao/parseurl.js/esm/parseURL'
// parseURL() 方法的核心实现,分别采用正则表达式和 URL 构造函数解析
import parseURLWithRegExp from '@yaohaixiao/parseurl.js/esm/parseURLWithRegExp'
import parseURLWithURLConstructor from '@yaohaixiao/parseurl.js/esm/parseURLWithURLConstructor'

// 使用基础路径
const url = parseURL('/yaohaixiao/parseurl.js?age=24', 'https://github.com')

// 获取查询字符串中 age 的值,如果没有则返回空
url.searchParams.get('age') // -> '24'

API Documentation

parseURL.js 提供了一个核心方法 parseURL().

parseURL(url[, base])

Parameters

url

Type: String

Default: ``

(必须)表示绝对或相对 URL 的字符串。如果 url 是相对 URL,则会将 base 用作基准 URL。如果 url 是绝对URL,则无论参数base是否存在,都将被忽略;

base

Type: String

Default: ``

(必须)表示基准 URL 的字符串。在 url 是相对 URL 时,它才会起效。如果未指定,则默认为 ”;

Returns

Type: Object

返回解析 URL 地址信息后的(JSON)对象:

const url = parseURL('https://yaohaixiao:parseUrl.js@github.com:8080/yaohaixiao/parseurl.js?col=24&row=23#top')
// ->
// {
//   href: "https://yaohaixiao:parseUrl.js@github.com:8080/yaohaixiao/parseurl.js?col=24&row=23#top",
//   origin:
//   "https://github.com:8080",
//   protocol: "https:",
//   username: "yaohaixiao",
//   password: "parseUrl.js",
//   hostname: "github.com",
//   port: "8080",
//   host: "github.com:8080",
//   pathname: "/yaohaixiao/parseurl.js",
//   search: "?col=24&row=23",
//   path: "/yaohaixiao/parseurl.js?col=24&row=23",
//   hash: "#top",
//   searchParams: {
//     get(prop) {
//      // ... 获取 search 数据的方法
//     }
//   }
//  }

License

Licensed under MIT License.

MIT License Copyright (c) 2023 Robert Yao Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

parseURL.js - 专门解析 URL 地址信息的 JavaScript 工具。 展开 收起
JavaScript
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/yaohaixiao/parseURL.js.git
git@gitee.com:yaohaixiao/parseURL.js.git
yaohaixiao
parseURL.js
parseURL.js
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891