JavaScript 的document对象中有一个location的子对象,其包括是属性如下:
- document.location.host //表示当前域名 + 端口号
- document.location.hostname //表示域名
- document.location.href //表示完整的URL
- document.location.port //表示端口号
- document.location.protocol //表示当前的网络协议
var getBaseUrl = function () {
//protocol 属性是一个可读可写的字符串,可设置或返回当前 URL 的协议,所有主要浏览器都支持 protocol 属性
var ishttps = 'https:' == document.location.protocol ? true: false;
var url = window.location.host;
if(ishttps){
url = 'https://' + url;
}else{
url = 'http://' + url;
}
return url;
}
本文介绍了JavaScript中document.location对象的各个属性,包括host、hostname、href、port及protocol,并提供了一个获取当前URL基本路径的示例函数。
1556

被折叠的 条评论
为什么被折叠?



