toAbsoluteUrl

Resolve a relative URL against a base URL and return the absolute form, so links built from fragments point to the right location.

Usage

toAbsoluteUrl converts a relative URL into an absolute URL.

import { toAbsoluteUrl } from '@movk/core'

toAbsoluteUrl('/path', 'https://example.com')
// 'https://example.com/path'

toAbsoluteUrl('../other', 'https://example.com/api/users')
// 'https://example.com/api/other'

toAbsoluteUrl('https://other.com', 'https://example.com')
// 'https://other.com' (already absolute, returned as-is)

API

toAbsoluteUrl(relativeUrl, baseUrl)

Convert a relative URL to an absolute URL.

Parameters

relativeUrl
string required
The relative URL.
baseUrl
string required
The base URL.

Returns

returns
string
The absolute URL, or the original string if conversion fails.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License