用 Javascript 可以单独获取当前域名、Url、相对路径和参数,所谓单独攻取,即域名不包括网页文件的路径和参数、参数不包括域名和网页文件路径,下面分别介绍。
 
  一、js获取当前域名有2种方法
  1、方法一
  var domain = document.domain; (这个不含端口 比如 localhost)
 
  2、方法二
  var domain = window.location.host; (这个包含端口 比如 localhost:81 )
判断域名含有某字符
|  | var str=window.location.host;| <script type="text/javascript"> | 
 | 
|  | //sz | 
|  | if(str.indexOf('szfz.com')>-1){ | 
|  |  | 
|  | var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://"); | 
|  | document.write(unescape("%3Cspan id='cnzz_stat_icon_4869877'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/stat.php%3Fid%3D4869877%26show%3Dpic' type='text/javascript'%3E%3C/script%3E")); | 
|  |  | 
|  | }//400 | 
|  | else{ | 
|  |  | 
|  |  | 
|  | var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://"); | 
|  | document.write(unescape("%3Cspan id='cnzz_stat_icon_4628712'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s22.cnzz.com/stat.php%3Fid%3D4628712%26show%3Dpic' type='text/javascript'%3E%3C/script%3E")); | 
|  |  | 
|  |  | 
|  | } | 
  
| <script> | 
 | 
|  | var str=window.location.host; | 
|  | if(str.indexOf('hodez.com')>-1 ){document.getElementById('idzhiBjxyd').innerHTML='地址:石嘴山市惠农区河滨工业园区北盛街001号'}; | 
|  | if(str.indexOf('ufeqa.com')>-1 ){document.getElementById('idzhiBjxyd').innerHTML='地址:北京市海淀区北三环西路甲30号双天大厦5层 '}; | 
|  | if(str.indexOf('edunsx.com')>-1 ){document.getElementById('idzhiBjxyd').innerHTML='地址:北京市海淀区东北旺西路8号中关村软件园21号启明星辰大厦'}; | 
|  | if(str.indexOf('bjlpxsw.com')>-1 ){document.getElementById('idzhiBjxyd').innerHTML='地址:深圳市南山区南头街道新安古城中山南一街二坊25号601'}; | 
|  | if(str.indexOf('eyourmind.com')>-1 ){document.getElementById('idzhiBjxyd').innerHTML='地址:广东省深圳市龙岗区龙城街道爱联社区田寮村21栋501'}; | 
|  | </script> | 
3、注意问题
  由于获取到的当前域名不包括 http://,所以把获取到的域名赋给 a 标签的 href 时,别忘了加上 http://,否则单击链接时导航会出错。
 
 
  二、获取当前Url的4种方法
  var url = window.location.href;
  var url = self.location.href;
  var url = document.URL;
  var url = document.location;
  ie 地址栏显示的是什么,获取到的 url 就是什么。
 
 
  三、获取当前相对路径的方法
  首先获取 Url,然后把 Url 通过 // 截成两部分,再从后一部分中截取相对路径。如果截取到的相对路径中有参数,则把参数去掉。
  function GetUrlRelativePath()
  {
    var url = document.location.toString();
    var arrUrl = url.split("//");
    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符
    if(relUrl.indexOf("?") != -1){
      relUrl = relUrl.split("?")[0];
    }
    return relUrl;
  }
  调用方法:GetUrlRelativePath();
  举例:假如当前 Url 是 http// www. liangshunet. com/pub/item.aspx?t=osw7,则截取到的相对路径为:/pub/item.aspx。
 
  四、获取当前Url参数的方法
  1、获取Url参数部分
  function GetUrlPara()
  {
    var url = document.location.toString();
    var arrUrl = url.split("?");
    var para = arrUrl[1];
    return para;
  }
  调用方法:GetUrlPara()
  举例:假如当前 Url 是 http// www. liangshunet. com/pub/item.aspx?t=osw7,则截取到的参数部分为:t=osw7。
 
  2、获取Url中指定参数的值请看《Js获取指定Url参数》一文。
来自 http://www.liangshunet.com/ca/201306/453800003.htm