js處理url參數

來源:互聯網
上載者:User

標籤:

var UrlArgent = {            Parsed: false, //是否已解析            Cache: {}, //緩衝值            ParseArg: function () {                // 解析地址欄的參數值                UrlArgent.Parsed = true;                var url = location.search;                if (-1 == url.indexOf(‘?‘)) return;                var args = url.substr(1).split("&");                for (var i = 0; i < args.length; i++) {                    var tmp = args[i];                    var pos = tmp.indexOf(‘=‘);                    if (-1 == pos) continue;                    UrlArgent.Cache[tmp.substr(0, pos)] = tmp.substr(pos + 1);                }            },            GetItem: function (queryStringName, defaultVal) {                // 擷取單個項目的值  queryStringName大小寫敏感,及區分大小寫                if (UrlArgent.Parsed == false) UrlArgent.ParseArg();                return UrlArgent.Cache[queryStringName] || (typeof (defaultVal) == ‘undefined‘ ? ‘‘ : defaultVal.toString());            },            GetItemIgnore: function (queryStringName, defaultVal) {                // 擷取單個項目的值  queryStringName忽略大小寫                if (UrlArgent.Parsed == false) UrlArgent.ParseArg();                                for (var k in UrlArgent.Cache) {                    if (UrlArgent.Cache.hasOwnProperty(k) == false) continue; //只取私人屬性                    if (k.toLowerCase() == queryStringName.toLowerCase()) return UrlArgent.Cache[k];                }                return (typeof (defaultVal) == ‘undefined‘ ? ‘‘ : defaultVal.toString());            },            GetUrl: function (newValue, ignore) {                // 擷取url參數 newValue增加或修改的值  ignore關鍵字是否忽略大小寫,true忽略大小寫其他值大小寫敏感                if (typeof (newValue) != ‘object‘) return location.search.substr(1); //原樣返回                if (UrlArgent.Parsed == false) UrlArgent.ParseArg();                var url = [];                for (var k in UrlArgent.Cache) {                    if (UrlArgent.Cache.hasOwnProperty(k) == false) continue; //只取私人屬性                    var val = UrlArgent.Cache[k]; //預設是原值                    for (var n in newValue) {                        if (newValue.hasOwnProperty(n) == false) continue;                        if ((k == n) || (ignore == true && k.toLowerCase() == n.toLowerCase())) { //需要替換原來的值                            val = newValue[n].toString(); //賦新值                            newValue[n] = null; //清除設定了值的項                        }                    }                    url.push(k + ‘=‘ + val);                }                for (var n in newValue) { //新增加的索引值                    if (newValue.hasOwnProperty(n) == false) continue;                    if (newValue[n] == null) continue;                    url.push(n + ‘=‘ + newValue[n].toString());                }                return url.join(‘&‘);            }        };

 

使用:

        var a = UrlArgent.GetItem(‘x‘);        var x = UrlArgent.GetItem(‘x‘, 0);        var y = UrlArgent.GetItemIgnore(‘X‘);        var s = UrlArgent.GetUrl();        var w = UrlArgent.GetUrl({ "x": "vv", "ismobilE": "vs" });        var z = UrlArgent.GetUrl({ "X": "vv", "ismobilE": "vs" }, true);

 

js處理url參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.