Jquery获取访问地址中参数

本文介绍了一种通过JavaScript解析URL中的查询参数,并根据这些参数来控制网页中指定元素的显示状态的方法。具体实现包括两种不同的写法,一种是直接操作URL参数,另一种是通过自定义函数获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

var url = window.location.href.split("?");
if(url.length>1){
    var theRequest = new Object();//theRequest为i获取的参数集合
    var strs = url[1].split('&');
    for(var i=0;i<strs.length;i++){
        theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
    }
    if( theRequest["xxxx"]=='true'){//获取相应参数进行相关操作
        $("#sidenav").attr("display","block");
        $("#topnav").attr("display","block");
    }else{
        $("#sidenav").attr("hidden","none");
        $("#topnav").attr("hidden","none");
    }
}
-------------------------------------

第二种写法

// 获取url 参数
window.getQueryString = function (name) {
   var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
   var r = window.location.search.substr(1).match(reg);
   if (r != null) {
      return unescape(r[2]);    //(r[2]);
   }else{
      var index1 = window.location.href.indexOf('?');
      if(index1>=0){
         tempURL= window.location.href.substring(index1+1);
         r = tempURL.match(reg);
         if(r == null){
                   var index2 = tempURL.indexOf('?');
                   if(index2){
                       r = tempURL.substring(index2+1).match(reg);
                   }
         }

      }else{
         r = window.location.href.substring(0).match(reg);
      }

      if (r != null) {
         return unescape(r[2]);    //(r[2]);
      }
   }
   return '';
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值