Skip to content

getAbsoluteUrl 获取绝对 URL

function getAbsoluteUrl(rawUrl: string): string

将相对路径或 URL 字符串规范化为绝对 URL。若输入已经是有效的 HTTP/HTTPS URL,会原样返回。

参数

参数名说明类型默认值
rawUrl待规范化的 URL 或相对路径string必填

返回值

返回值说明类型
absoluteUrl绝对 URL;空输入返回空字符串string

示例

typescript
import { getAbsoluteUrl } from '@mingto/tools'

getAbsoluteUrl('https://example.com/a.png')
// 'https://example.com/a.png'

getAbsoluteUrl('/assets/a.png')
// 基于当前页面地址生成绝对 URL

注意事项

  • 相对路径转换依赖浏览器 window.location.href
  • URL 有效性判断基于 isValidUrl