Web探索|Asp.net||Jquery|MVC

Web前沿技术、移动解决方案
  博客园  :: 首页  :: 新随笔  :: 管理

js 获取地址栏参数,支持多参数

Posted on 2012-08-09 09:05  reckcn  阅读(245)  评论(0编辑  收藏  举报
1.============================================================================================
function GetRequest() {
            var url = location.search; //获取url中"?"符后的字串   
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }

调用方法:

 $(document).ready(function () {
            var Request = new Object();
            Request = GetRequest();
            carcode = Request['code']; //会员卡号
            szb = Request['szb']; //市值币余额
            strSZBtoWGB = Request['szbtowgb'];
            strSZBtoRMB = Request['szbtormb'];
            $("#tbxSZB").val(szb); //对文本框赋值
        });

2.==============================================================================================

$.GetQueryString = function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    return r != null ? unescape(r[2]) : "";
};

调用:$.GetQueryString("name")