Js+Jq获取URL参数的集中方法示例代码

  JQ取值方法:

  jquery本身也不存在取得URL参数的方法,但是已经存在插件,可以直接取得URL等参数

  插件连接主页:https://github.com/allmarkedup/jQuery-URL-Parser周口银屑病专科医院

  下载链接:http://download.github.com/allmarkedup-jQuery-URL-Parser-bb2bf37.zip

  Examples of use

  Using the current page's url (for these examples https://mysite.com/information/about/index.html?itemID=2&user=dave):

  // get the protocol

  jQuery.url.attr("protocol") // returns 'http'

  // get the path

  jQuery.url.attr("path") // returns '/information/about/index.html'

  // get the host

  jQuery.url.attr("host") // returns 'mysite.com'

  // get the value for the itemID query parameter

  jQuery.url.param("itemID") // returns 2

  // get the second segment from the url path

  jQuery.url.segment(2) // returns 'about'

  Using a different url to the current page:

  // set a different URL and return the anchor string

  jQuery.url.setUrl("http://allmarkedup.com/category/javascript/#footer").attr("anchor") // returns 'footer'

  JS原生获取:

  最原始的JS方法:

  复制代码代码如下:

  var URLParams = new Array();

  var aParams = document.location.search.substr(1).split('&');

  for (i=0; i < aParams.length ; i++){

  var aParam = aParams[i].split('=');

  URLParams[aParam[0]] = aParam[1];

  }

  这样调用: 周口最好的牛皮癣医院

  http://127.0.0.1/index.php?name=name1&cid=123

  //取得传过来的name参数

  name=URLParams["name"];

  document.write(name);

  //取得传过来的cid

  cid=URLParams["cid"];

  正则分析法:

  方法一:

  复制代码代码如下:

  function getQueryString(name) {

  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");

  var r = window.location.search.substr(1).match(reg);

  if (r != null) return unescape(r[2]); return null;

  }

  这样调用:

  复制代码代码如下:

  alert(GetQueryString("参数名1"));

  alert(GetQueryString("参数名2"));

  alert(GetQueryString("参数名3"));

  方法二:周口最好的银屑病医院

  复制代码代码如下:

  

  这样调用:

  复制代码代码如下:

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值