以下代码整理于网络

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。

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

  1. js获取当前域名、Url、相对路径和参数以及指定参数

    一.js获取当前域名有2种方法 1.方法一 var domain = document.domain; 2.方法二 var domain = window.location.host; 3.注意问题 ...

  2. 使用js获取浏览器地址栏里的参数

    用JS获取地址栏参数的方法(超级简单) 方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var reg = new ...

  3. JS获取浏览器地址栏的多个参数值的任意值

    getParamValue("id"); //http://localhost:2426/TransactionNotes.aspx?id=100 //返回值是100: // 根据 ...

  4. js获取当前页面的网址域名地址

    1.获取当前完整网址thisURL = document.URL;thisHREF = document.location.href;thisSLoc = self.location.href;thi ...

  5. 使用JS获取上一页的url地址

    一般来说每个页面上面都有一个返回按钮,用来返回上一页,代码如下: <a href="javascript:history.go(-1)" class="jsBack ...

  6. js获取或设置当前窗口url参数

    直接上代码 // 获取当前窗口url中param参数的值 function get_param(param){ var query = location.search.substring(1).spl ...

  7. JS获取浏览器地址栏的多参数值的任意值

    常用的几个方法就不讲了,这里我用的是两个方法组 使用方法是: getParamValue("id"); http://localhost:2426/TransactionNotes ...

  8. js获取来源和当前域名

    参考:http://www.cnblogs.com/zuosong160522/p/5755615.html http://www.oicqzone.com/pc/2014113020362.html

  9. js获取浏览器地址栏传递的参数

    function getQueryString(key){ var href=window.location.href; var reg = new RegExp(key +"=([^&am ...

随机推荐

  1. LightOJ - 1079 Just another Robbery —— 概率、背包

    题目链接:https://vjudge.net/problem/LightOJ-1079 1079 - Just another Robbery    PDF (English) Statistics ...

  2. SQL语句编写注意问题

    下面就某些SQL语句的where子句编写中需要注意的问题作详细介绍.在这些where子句中,即使某些列存在索引,但是由于编写了劣质的SQL,系统在运行该SQL语句时也不能使用该索引,而同样使用全表扫描 ...

  3. 杂草丛生HTML5网站模板

    杂草丛生HTML5个人网站模板是一款野草到处生长的HTML5网站模板下载. 模板地址:http://www.huiyi8.com/sc/8780.html

  4. openfire调试环境

    导入工程: File->New->project: 选择“Java project from existing ant buildfile” 再从菜单windows->show vi ...

  5. codeforces 658D D. Bear and Polynomials(数学)

    题目链接: D. Bear and Polynomials time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. CF785CAnton and Permutation(分块 动态逆序对)

    Anton likes permutations, especially he likes to permute their elements. Note that a permutation of  ...

  7. MySQL活动期间制定月份注册用户下单情况_20161029

    在10.29到10.31号期间 10月新注册的用户订单金额满600元赠与优惠券 #3天内订单满600元且10月注册的用户订单明细 SELECT a.城市,a.用户ID,b.用户名称,DATE(b.注册 ...

  8. jmeter的http post请求与测试Java请求

    1.jmeter 测试Java请求 1.1 建立测试类,在被测程序中添加测试类 1.2 将测试程序打包,打成不可运行的包 1.3 将打好的包,放在$JMETER_HOME/lib/exts下面,把测试 ...

  9. 查看SELinux状态&关闭SELinux

    1. 查看SELinux状态 1.1 getenforce getenforce 命令是单词get(获取)和enforce(执行)连写,可查看selinux状态,与setenforce命令相反. se ...

  10. C语言单精度浮点型转换算法

    文章来源:http://blog.csdn.NET/educast/article/details/8522818 感谢原作者. 关于16进制浮点数对于大小为32-bit的浮点数(32-bit为单精度 ...