js获取当前地址栏的域名、Url、相对路径和参数以及指定参数

以下代码整理于网络

1、设置或获取对象指定的文件名或路径。
window.location.pathname
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.pathname); 则输出:/topic/index

2、设置或获取整个 URL 为字符串。
window.location.href
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.href); 则输出:http://localhost:8086/topic/index?topicId=361

3、设置或获取与 URL 关联的端口号码。
window.location.port
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.port); 则输出:8086

4、设置或获取 URL 的协议部分。
window.location.protocol
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.protocol); 则输出:http:

5、设置或获取 href 属性中在井号“#”后面的分段。
window.location.hash

6、设置或获取 location 或 URL 的 hostname 和 port 号码。
window.location.host
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.host); 则输出:http:localhost:8086

7、设置或获取 href 属性中跟在问号后面的部分。
window.location.search
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.search); 则输出:?topicId=361

window.location
属性                  描述
href                  设置或获取整个 URL 为字符串。
host                  设置或获取 location 或 URL 的 hostname 和 port 号码。
protocol              设置或获取 URL 的协议部分。
port                  设置或获取与 URL 关联的端口号码。
hostname              设置或获取 location 或 URL 的主机名称部分。
pathname              设置或获取对象指定的文件名或路径。
search                设置或获取 href 属性中跟在问号后面的部分。
hash                  设置或获取 href 属性中在井号“#”后面的分段。

 一、js获取当前域名有2种方法

1、方法一

  var domain = document.domain;

 

  2、方法二

  var domain = window.location.host;

 

  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。

  五、获取指定Url参数的方法

//paraName 等找参数的名称
  function GetUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");

    if (arrObj.length > 1) {
      var arrPara = arrObj[1].split("&");
      var arr;

      for (var i = 0; i < arrPara.length; i++) {
        arr = arrPara[i].split("=");

        if (arr != null && arr[0] == paraName) {
          return arr[1];
        }
      }
      return "";
    }
    else {
      return "";
    }
  }

  调用方法:GetUrlParam("id");

  举例说明:

  假如当网页的网址有这样的参数 test.htm?id=896&s=q&p=5,则调用 GetUrlParam("p"),返回 5。

猜你喜欢

转载自www.cnblogs.com/GGYC/p/10661050.html
今日推荐