# js-url-helper

> An JavaScript assistant for Browser URL

Latest version **0.2.2** (published 2020-12-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install js-url-helper
pnpm add js-url-helper
yarn add js-url-helper
bun add js-url-helper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2020-12-25 |
| First published | 2018-06-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 26.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | alex86gbk |
| Maintainers | alexbian |
| Keywords | url, helper |

## Links

- npm: https://www.npmjs.com/package/js-url-helper
- Repository: https://github.com/alex86gbk/js-url-helper
- Homepage: https://github.com/alex86gbk/js-url-helper#readme
- Issues: https://github.com/alex86gbk/js-url-helper/issues
- npm.io page: https://npm.io/package/js-url-helper

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 0.2.2 (latest) — 2020-12-25
- 0.2.1 — 2020-12-10
- 0.2.0 — 2020-12-10
- 0.1.4-beta — 2019-06-12
- 0.1.4 — 2019-06-12
- 0.1.3 — 2018-11-01
- 0.1.2 — 2018-08-24
- 0.1.1 — 2018-07-20
- 0.1.0 — 2018-06-20

## README

# introduce
## 介绍

Js-url-helper 是一个浏览器 `URL` 辅助工具。
通过 Js-url-helper 可以很方便的操作 `location` 对象，获取，设置页面跳转参数。

如：
 - 获取/设置 `search` 参数
 - 获取/设置 `hash` 参数
 - 执行 `URL` 跳转
 - 生成 `URL` 链接
 
[API 说明文档](#api)

# download
## 下载
（仅下载至本地后使用，不能直接作为 CDN 资源引用）

未压缩[unzipped](https://raw.githubusercontent.com/alex86gbk/js-url-helper/master/dist/js-url-helper.js)

压缩[(~zipped)](https://raw.githubusercontent.com/alex86gbk/js-url-helper/master/dist/js-url-helper.min.js)

# install
安装

# npm
通过 npm

```bash
npm install --save js-url-helper
```

# browser
浏览器环境：

```html
<script src="js-url-helper.js"></script>
```

# api

* [实例化 UrlHelper](README.md#urlhelper)
* [获取 search 参数](README.md#getsearchparam)
* [获取 hash 参数](README.md#gethashparam)
* [设置 search 参数](README.md#setsearchparam)
* [设置 hash 参数](README.md#sethashparam)
* [生成 URL 链接](README.md#link)
* [执行 URL 跳转](README.md#jump)

# UrlHelper

## 实例化

`new UrlHelper(location)`

### Arguments

 - location **{Object}** 浏览器 `location` 对象
 
### Returns

 - **{Object}** UrlHelper 实例

### Example
```javascript
var urlHelper = new UrlHelper(location);
```

# getSearchParam

`getSearchParam(location)`

## 获取 search 参数

### Arguments

 - [location] `可选参数` **{Object}** 浏览器 `location` 对象
 
### Returns

 - **{Object}** `search` 参数对象

### Example
```text
'http://www.example.com/path/index.html?query1=test&silly=willy'
```

```javascript    
var searchParam = urlHelper.getSearchParam();
// {query1: 'test', silly: 'willy'}
```

# getHashParam

`getHashParam(location)`

## 获取 hash 参数

### Arguments

 - [location] `可选参数` **{Object}** 浏览器 `location` 对象

### Returns

 - **{Object}** `hash` 参数对象

### Example
```text
'http://www.example.com/path/index.html#test=hash&chucky=cheese'
```

```javascript    
var hashParam = urlHelper.getHashParam();
// {test: 'hash', chucky: 'cheese'}
```

# setSearchParam

`setSearchParam(param)`

## 设置 search 参数

### Arguments

 - param **{Object}** `search` 对象
 
### Returns
 
 - **{String}** 带 `search` 信息的 `URL` 参数字符串

### Example
```javascript
var searchParamString = urlHelper.setSearchParam({
  query1: 'test1',
  silly: 'willy'
});
// '?query1=test1&silly=willy'
```

# setHashParam

`setHashParam(param)`

## 设置 hash 参数

### Arguments

 - param **{Object}** `hash` 对象
 
### Returns
 
 - **{String}** 带 `hash` 信息的 `URL` 参数字符串

### Example
```javascript
var hashParamString = urlHelper.setHashParam({
  test: 'hash',
  chucky: 'cheese'
});
// '#test=hash&chucky=cheese'
```

# link

`link(param)`

## 生成 URL 链接，参数为空为当前页。

### Arguments

 - param **{Object}**
 - param.path **{String}** 跳转路径
 - param.search **{String}** search参数
 - param.hash **{String}** hash参数
 
### Returns
 
 - **{String}** 链接地址

### Example
```javascript
var link = urlHelper.link({
  path: '/path/other',
  search: urlHelper.setSearchParam({
    chapterId: 1
  }),
  hash: urlHelper.setHashParam({
    questionId: 2
  })
});

或

var link = urlHelper.link({
  path: '/path/other',
  search: {
    chapterId: 1
  },
  hash: {
    questionId: 2
  }
});

document.getElementById('nextQuestion').href = link;
```

# jump

`jump(param)`

## 执行 URL 跳转，参数为空则刷新当前页。

### Arguments

 - param **{Object}**
 - param.path **{String}** 跳转路径
 - param.search **{String}** search参数
 - param.hash **{String}** hash参数
 
### Returns
 
 - **无**

### Example
```javascript
urlHelper.jump({
  path: '/path/other',
  search: urlHelper.setSearchParam({
    chapterId: 1
  }),
  hash: urlHelper.setHashParam({
    questionId: 2
  })
});

或

urlHelper.jump({
  path: '/path/other',
  search: {
    chapterId: 1
  },
  hash: {
    questionId: 2
  }
});
```

# 扩展阅读

## location

>Location 对象包含有关当前 URL 的信息。

>Location 对象是 Window 对象的一个部分，可通过 window.location 属性来访问。

## protocol

>设置或返回当前 URL 的协议。

## host

>设置或返回主机名和当前 URL 的端口号。

## port

>设置或返回当前 URL 的端口号。

## pathname

>设置或返回当前 URL 的路径部分。

## search

>设置或返回从问号 (?) 开始的 URL（查询部分）。

## hash

>设置或返回从井号 (#) 开始的 URL（锚）。

## href

>设置或返回完整的 URL。

---
_Source: https://npm.io/package/js-url-helper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
