我需要从给定的URL中提取完整的协议、域和端口。例如:
https://localhost:8181/ContactUs-1.0/contact?lang=it&report_type=consumer
>>>
https://localhost:8181
我需要从给定的URL中提取完整的协议、域和端口。例如:
https://localhost:8181/ContactUs-1.0/contact?lang=it&report_type=consumer
>>>
https://localhost:8181
当前回答
使用ES6模板文字:
Const url = ' ${location.protocol}//${location.hostname}${location.port?':'+location.port: "} '; . getelementbyid(“结果”)。innerText = url; < div id = "结果" > < / div >
你可以简化为:
Const url = ' ${location.protocol}//${location.host} '; . getelementbyid(“结果”)。innerText = url; < div id = "结果" > < / div >
其他回答
const full = location.protocol + '//' + location.host;
var getBasePath = function(url) {
var r = ('' + url).match(/^(https?:)?\/\/[^/]+/i);
return r ? r[0] : '';
};
首先获取当前地址
var url = window.location.href
然后解析这个字符串
var arr = url.split("/");
你的网址是:
var result = arr[0] + "//" + arr[2]
host
var url = window.location.host;
返回localhost: 2679
主机名
var url = window.location.hostname;
返回本地主机
Window.location.protocol + '//' + window.location.host