淺談JavaScript中你可能不知道URL構(gòu)造函數(shù)的屬性
URL
URL 是統(tǒng)一資源定位符,對(duì)可以從互聯(lián)網(wǎng)上得到的資源的位置和訪問方法的一種簡(jiǎn)潔的表示,是互聯(lián)網(wǎng)上標(biāo)準(zhǔn)資源的地址。互聯(lián)網(wǎng)上的每個(gè)文件都有一個(gè)唯一的 URL,它包含的信息指出文件的位置以及瀏覽器應(yīng)該怎么處理它,
在 Web 開發(fā)中,有許多情況需要解析 URL,這篇主要學(xué)習(xí)如何使用 URL 對(duì)象實(shí)現(xiàn)這一點(diǎn)
例如,這里是這篇博客文章的路徑:
https://www.vipbic.com/thread.html?id=101
通常您需要訪問 URL 的特定屬性。這些可能是主機(jī)名(例如 vipbic.com ) ,或者路徑名(例如/thread)
JavaScript用于訪問URL對(duì)象的提供一個(gè)URL()構(gòu)造函數(shù),很方便解析
一個(gè)完整URL
用一張圖片來解釋,沒有太多的文字描述,在下面的圖片中你可以找到一個(gè) URL 的主要包含屬性:

URL constructor
URL ()是一個(gè) constuctor 函數(shù),它可以解析 URL 的對(duì)象:
const url = new URL(relativeOrAbsolute [, absoluteBase]);
relativeOrAbsolute參數(shù)可以是絕對(duì) URL,也可以是相對(duì) URL。如果第一個(gè)參數(shù)是相對(duì)的,那么第二個(gè)參數(shù) absoluteBase 必須是絕對(duì) URL,它必須是第一個(gè)參數(shù)的基礎(chǔ)
例如,讓我們用一個(gè)絕對(duì) URL 初始化 URL():
const url = new URL('http://example.com/path/index.html');
url.href; // => 'http://example.com/path/index.html'
或者合并相對(duì)和絕對(duì)的 url:
const url = new URL('/path/index.html', 'http://example.com');
url.href; // => 'http://example.com/path/index.html'
創(chuàng)建 URL ()實(shí)例后,可以訪問實(shí)例:
interface URL {
href: USVString;
protocol: USVString;
username: USVString;
password: USVString;
host: USVString;
hostname: USVString;
port: USVString;
pathname: USVString;
search: USVString;
hash: USVString;
readonly origin: USVString;
readonly searchParams: URLSearchParams;
toJSON(): USVString;
}
可以嘗試在瀏覽中打印

Query string
Search 屬性訪問前綴為? : 的 URL 的查詢字符串:
const url = new URL( 'http://example.com/path/index.html?message=hello&who=world' ); url.search; // => '?message=hello&who=world'
如果查詢字符串不存在的字符串,url.search 將返回為空字符串” :
const url1 = new URL('http://example.com/path/index.html');
const url2 = new URL('http://example.com/path/index.html?');
url1.search; // => ''
url2.search; // => ''
Parsing query string

訪問查詢參數(shù)比訪問原始查詢字符串更方便
一種簡(jiǎn)單的查詢參數(shù)選擇方法提供了 url.searchParams 屬性,該屬性包含 URLSearchParams 的實(shí)例
URLSearchParams 對(duì)象提供了許多方法(如 get (param)、 has (param))來訪問查詢字符串參數(shù)
看一個(gè)例子:
const url = new URL(
'http://example.com/path/index.html?message=hello&who=world'
);
url.searchParams.get('message'); // => 'hello'
url.searchParams.get('missing'); // => null
get.('message'),返回消息查詢參數(shù)的值-‘ hello',當(dāng)去嘗試,訪問一個(gè)不存在的參數(shù) url.searchParams.get('missing')的結(jié)果為 null
hostname
Hostname 屬性包含 URL 的主機(jī)名:
const url = new URL('http://example.com/path/index.html');
url.hostname; // => 'example.com'
pathname
屬性獲取 URL 的路徑名:
const url = new URL('http://example.com/path/index.html?param=value');
url.pathname; // => '/path/index.html'
如果 URL 沒有路徑,URL.pathname 屬性將返回斜杠字符/:
const url = new URL('http://example.com/');
url.pathname; // => '/'
hash
可以使用 url.hash 屬性訪問#后面的參數(shù):
const url = new URL('http://example.com/path/index.html#bottom');
url.hash; // => '#bottom'
當(dāng) URL 中的散列#時(shí),URL.hash 計(jì)算為空字符串” :
const url = new URL('http://example.com/path/index.html');
url.hash; // => ''
URL validation
當(dāng)new URL ()構(gòu)造函數(shù)創(chuàng)建一個(gè)實(shí)例時(shí),作為副作用,它還驗(yàn)證 URL 的正確性。如果 URL 值無效,則拋出 TypeError
例如,http ://example. com 是一個(gè)無效的 URL,因?yàn)?http 后面的空格字符
讓我們使用這個(gè)無效的 URL 來初始化解析器:
try {
const url = new URL('http ://example.com');
} catch (error) {
error; // => TypeError, "Failed to construct URL: Invalid URL"
}
因?yàn)?http ://example. com'是一個(gè)無效的 URL,正如預(yù)期的那樣,new URL ('http ://example. com')拋出一個(gè) TypeError
URL manipulation
除了訪問 URL 屬性之外,搜索、主機(jī)名、路徑名、hash等屬性都是可寫的ーー因此您可以操作 URL
例如,讓我們把現(xiàn)有 URL 的主機(jī)名從 red. com 修改為 blue.io:
const url = new URL('http://red.com/path/index.html');
url.href; // => 'http://red.com/path/index.html'
url.hostname = 'blue.io';
url.href; // => 'http://blue.io/path/index.html'
注意,只有 URL ()實(shí)例的 origin 和 searchParams 屬性是只讀的。其他的都是可寫的,當(dāng)你改變它們的時(shí)候可以修改 URL
總結(jié)
URL()構(gòu)造函數(shù)可以方便地在 JavaScript 中解析(和驗(yàn)證) URL
new URL (relativeOrAbsolute [ ,absolute base ])接受作為第一個(gè)參數(shù)的絕對(duì)或相對(duì) URL。如果第一個(gè)參數(shù)是相對(duì)的,則必須將第二個(gè)參數(shù)指
示為一個(gè)作為第一個(gè)參數(shù)基礎(chǔ)的URL
創(chuàng)建 URL()實(shí)例后,可以獲取到以下實(shí)列方法
- url.search 原始查詢字符串
- url.searchParams 選擇查詢字符串參數(shù)
- url.hostname 訪問主機(jī)名
- url.pathname 讀取路徑名
- url.hash #后面的參數(shù)
文章屬于翻譯,作者部分有所改動(dòng),
作者:羊先生
英文原文, https://dmitripavlutin.com/parse-url-javascript/
到此這篇關(guān)于淺談JavaScript中你可能不知道URL構(gòu)造函數(shù)的屬性的文章就介紹到這了,更多相關(guān)JavaScript URL構(gòu)造函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript的switch用法注意事項(xiàng)分析
這篇文章主要介紹了javascript的switch用法注意事項(xiàng),實(shí)例分析了switch語句進(jìn)行判定的原理與使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02
javascript基礎(chǔ)語法學(xué)習(xí)筆記
這篇文章主要為大家分享了javascript基礎(chǔ)語法學(xué)習(xí)筆記,幫助大家夯實(shí)javascript基礎(chǔ)知識(shí),感興趣的小伙伴們可以參考一下2016-01-01
js+html+css實(shí)現(xiàn)簡(jiǎn)單電子時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了js+html+css實(shí)現(xiàn)簡(jiǎn)單電子時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
js實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)js實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02
基于JavaScript實(shí)現(xiàn)在新的tab頁(yè)打開url
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)在新的tab頁(yè)打開url 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
CocosCreator通用框架設(shè)計(jì)之資源管理
這篇文章主要介紹了CocosCreator通用框架設(shè)計(jì)之資源管理,對(duì)性能優(yōu)化感興趣的同學(xué),一定要看一下2021-04-04

