我需要从给定的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
当前回答
host
var url = window.location.host;
返回localhost: 2679
主机名
var url = window.location.hostname;
返回本地主机
其他回答
var getBasePath = function(url) {
var r = ('' + url).match(/^(https?:)?\/\/[^/]+/i);
return r ? r[0] : '';
};
const full = location.protocol + '//' + location.host;
Window.location.origin就足以得到相同的。
实际上,window.location.origin在遵循标准的浏览器中工作得很好,但你猜怎么着。IE没有遵循标准。
正因为如此,我在IE、FireFox和Chrome浏览器中使用了这个方法:
var full = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');
但为了将来可能引起冲突的增强,我在“location”对象之前指定了“window”引用。
var full = window.location.protocol+'//'+window.location.hostname+(window.location.port ? ':'+window.location.port: '');
host
var url = window.location.host;
返回localhost: 2679
主机名
var url = window.location.hostname;
返回本地主机