可以使用 JavaScript 中的 String.prototype.match() 方法來執(zhí)行匹" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁 > 營銷資訊 > 建站知識 > 客服系統(tǒng)前端開發(fā):JavaScript獲取URL中的協(xié)議部分和域名部分【唯一客服】網(wǎng)頁

客服系統(tǒng)前端開發(fā):JavaScript獲取URL中的協(xié)議部分和域名部分【唯一客服】網(wǎng)頁

時間:2023-02-01 02:28:01 | 來源:建站知識

時間:2023-02-01 02:28:01 來源:建站知識

在客服系統(tǒng)中如果想要鏈接websocket需要確定是ws:// 還是wss:// 所以,我封裝了兩個函數(shù),用于獲取URL中的協(xié)議是HTTP 還是HTTPS ,以及獲取到域名部分

可以使用 JavaScript 中的 String.prototype.match() 方法來執(zhí)行匹配操作,并使用第一個捕獲組來獲取匹配的域名部分。

//獲取協(xié)議部分function getProtocolFromUrl(url) { if(url==""){ url=window.location.href; } const regex = /^(https?)/i; const match = url.match(regex); return match[1];}console.log(getProtocolFromUrl("https://gofly.v1kf.com")); // "https"console.log(getProtocolFromUrl("http://gofly.v1kf.com/")); // "http"console.log(getProtocolFromUrl("http://gofly.v1kf.com")); // "http"console.log(getProtocolFromUrl("http://www.baidu.com/sdsdsds")); // "http"這樣就能判斷是使用ws還是wss去鏈接websocket

還要獲取域名部分

//獲取域名部分function getDomainFromUrl(url) { if(url==""){ url=window.location.href; } const regex = /^https?:////([^//]+)/i; const match = url.match(regex); return match[1];}console.log(getDomainFromUrl("")); // 當(dāng)前頁面的域名console.log(getDomainFromUrl("https://gofly.v1kf.com")); // "gofly.v1kf.com"console.log(getDomainFromUrl("http://gofly.v1kf.com/")); // "gofly.v1kf.com"console.log(getDomainFromUrl("http://gofly.v1kf.com")); // "gofly.v1kf.com"console.log(getDomainFromUrl("http://www.baidu.com/sdsdsds")); // "www.baidu.com"實(shí)際項(xiàng)目中的使用





唯一在線客服系統(tǒng)
https://gofly.v1kf.com


十年開發(fā)經(jīng)驗(yàn)程序員,離職全心創(chuàng)業(yè)中,歷時三年開發(fā)出的產(chǎn)品《唯一客服系統(tǒng)》

關(guān)鍵詞:部分,協(xié)議,系統(tǒng),獲取

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉