如果獲取“當(dāng)前”域名
host = window.location.host;
url=document.domain;
url = window.location.href;
取得完整url路徑: 用以下代碼可以完整研證結(jié)果:
thisDLoc = document.location;
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
還有一種稍有些復(fù)雜的取域名的方法,也是過濾了文件夾名,文件名,參數(shù)……
var getHost = function(url)
{
var host = "null";
if(typeof url == "undefined"|| null == url)
{
url = window.location.href;
}
var regex = /.*\:\/\/([^\/]*).*/;
var match = url.match(regex);
if(typeof match != "undefined" && null != match)
{
host = match[1];
}
return host;
}