時間:2023-02-01 02:28:01 | 來源:建站知識
時間:2023-02-01 02:28:01 來源:建站知識
在客服系統(tǒng)中如果想要鏈接websocket需要確定是ws:// 還是wss:// 所以,我封裝了兩個函數(shù),用于獲取URL中的協(xié)議是HTTP 還是HTTPS ,以及獲取到域名部分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)目中的使用關(guān)鍵詞:部分,協(xié)議,系統(tǒng),獲取
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。