专业编程基础技术教程

网站首页 > 基础教程 正文

jquery获取url参数及url加参数的方法

ccvgpt 2024-07-24 11:11:19 基础教程 22 ℃

1、url 取值和加参数的方法

$(document).off('click','.js-editReload').on('click','.js-editReload',function(event){
var $t=$(this), $newid=$t.attr("data-reload"),thisURL=window.location.href;
if (thisURL.indexOf("markvid=") > 0) {
  var oldid=$.Request('markvid');//$.Request 是调用方法,获取地址栏目的参数
  if (oldid != null) {
    thisURL=thisURL.replace("markvid="+oldid,"markvid="+$newid);
  } else {
    thisURL=thisURL.replace("markvid=","markvid="+$newid);
  }
} else {
  if (thisURL.indexOf("?") > 0) {
    thisURL = thisURL + "&markvid=" + $newid;
  }
  else {
    thisURL = thisURL + "?markvid=" + $newid;
  }
}
// location.href = thisURL;
thisChangeURL=thisURL; 
});

2、jquery 取url参数和在url加参数(直接调用即可)

(function (e) {
    e.extend({
        Request: function (e) {
            var n = location.search.match(new RegExp("[?&]" + e + "=([^&]*)(&?)", "i"));
            return n ? n[1] : n
        }
    })
})(jQuery);
//取值操作 www.baidu.com?act=1
console.log($.Request("act")) // 1

3、UrlDecode解码及UrlEncode编码的jQuery方法

地址栏中,若有中文,比如www.baidu.com?mid=ff芳芳
控制台打印mid值为:ff%E8%8A%B3%E8%8A%B3
可使用$('#name').text(decodeURI($.Request("mid")) );//ff芳芳
Jquery解码:decodeURIComponent(url);
Jquery编码:encodeURIComponent(url);

最近发表
标签列表