码迷,mamicode.com
首页 > Web开发 > 详细

js url参数的获取和设置以及删除

时间:2015-10-23 18:24:05      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

js url参数的获取和设置以及删除

 

 //获取url参数的值:name是参数名
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return (r[2]);
            }
            return null;
        }
 
//设置url参数值,ref参数名,value新的参数值
        function changeURLPar(url, ref, value)
        {
            var str = "";
            if (url.indexOf(‘?‘) != -1)
                str = url.substr(url.indexOf(‘?‘) + 1);
            else
                return url + "?" + ref + "=" + value;
            var returnurl = "";
            var setparam = "";
            var arr;
            var modify = "0";
            if (str.indexOf(‘&‘) != -1) {
                arr = str.split(‘&‘);
                for (i in arr) {
                    if (arr[i].split(‘=‘)[0] == ref) {
                        setparam = value;
                        modify = "1";
                    }
                    else {
                        setparam = arr[i].split(‘=‘)[1];
                    }
                    returnurl = returnurl + arr[i].split(‘=‘)[0] + "=" + setparam + "&";
                }
                returnurl = returnurl.substr(0, returnurl.length - 1);
                if (modify == "0")
                    if (returnurl == str)
                        returnurl = returnurl + "&" + ref + "=" + value;
            }
            else {
                if (str.indexOf(‘=‘) != -1) {
                    arr = str.split(‘=‘);
                    if (arr[0] == ref) {
                        setparam = value;
                        modify = "1";
                    }
                    else {
                        setparam = arr[1];
                    }
                    returnurl = arr[0] + "=" + setparam;
                    if (modify == "0")
                        if (returnurl == str)
                            returnurl = returnurl + "&" + ref + "=" + value;
                }
                else
                    returnurl = ref + "=" + value;
            }
            return url.substr(0, url.indexOf(‘?‘)) + "?" + returnurl;
        }

        //删除参数值
        function delQueStr(url, ref) {
            var str = "";
            if (url.indexOf(‘?‘) != -1) {
                str = url.substr(url.indexOf(‘?‘) + 1);
            }
            else {
                return url;
            }
            var arr = "";
            var returnurl = "";
            var setparam = "";
            if (str.indexOf(‘&‘) != -1) {
                arr = str.split(‘&‘);
                for (i in arr) {
                    if (arr[i].split(‘=‘)[0] != ref) {
                        returnurl = returnurl + arr[i].split(‘=‘)[0] + "=" + arr[i].split(‘=‘)[1] + "&";
                    }
                }
                return url.substr(0, url.indexOf(‘?‘)) + "?" + returnurl.substr(0, returnurl.length - 1);
            }
            else {
                arr = str.split(‘=‘);
                if (arr[0] == ref) {
                    return url.substr(0, url.indexOf(‘?‘));
                }
                else {
                    return url;
                }
            }
        }

  

js url参数的获取和设置以及删除

标签:

原文地址:http://www.cnblogs.com/wicub/p/4904852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!