js 批量获取浏览器参数

以前获取浏览器参数是这样的

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

// 调用方法
alert(GetQueryString("参数名1"));

这种方法适合获取单个的参数,但是当参数一多的时候就会很麻烦,有没有办法将浏览器的参数都放到一个对象里面呢?于是我就写了下面的这个方法

function getParameters() {                    //获取地址栏参数不包括hash值
            var search = decodeURI(location.search);//防止中文乱码
            search = search.replace(‘?‘, ‘‘);
            var searray = search.split("&");
            var obj = {}
            searray.map(function (item) {
                var sarry = item.split(‘=‘);
                obj[sarry[0]] = sarry[1];
            })
            return obj;
}

var para=getParameters();

是不是很简单,也很方便

恩,然后第一个获取单个的时候有优势,一个在获取所有的时候有优势,所以能不能整合一下呢?

好的,说干就干,修改下

function getParameters(obj) {//获取地址栏参数不包括hash值
            var search = decodeURI(location.search).replace(‘?‘, ‘‘);//防止中文乱码
            if(typeof obj=="string"){
                var reg = new RegExp("(^|&)"+ obj +"=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if(r!=null)return  unescape(r[2]); return null;
            }else if(typeof obj=="undefined"){
                var searray = search.split("&");
                var obj = {}
                searray.map(function (item) {
                    var sarry = item.split(‘=‘);
                    obj[sarry[0]] = sarry[1];
                })
                return obj;
            }

}

// getParameters();
//  getParameters(‘tn‘);

不满足的你是不是又在想我既不想获取一个也不想获取全部,我就想获取其中的几个呢?

谁让我这么爱你呢,满足你。

所以最终代码变成了这样

function getParameters(obj) {//获取地址栏参数不包括hash值
            var search = decodeURI(location.search).replace(‘?‘, ‘‘);//防止中文乱码
            if(typeof obj=="string"){
                var reg = new RegExp("(^|&)"+ obj +"=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if(r!=null)return  unescape(r[2]); return null;
            }else if(typeof obj=="undefined"){
                var searray = search.split("&");
                var obj = {}
                searray.map(function (item) {
                    var sarry = item.split(‘=‘);
                    obj[sarry[0]] = sarry[1];
                })
                return obj;
            }else if(obj.constructor ==Array){
                var searray = search.split("&");
                var fromarray = {}
                searray.map(function (item) {
                    var sarry = item.split(‘=‘);
                     if(obj.indexOf(sarry[0])>-1){
                          fromarray[sarry[0]] = sarry[1];
                     }

                })
                return fromarray;
            }

}

// getParameters();
//  getParameters(‘tn‘);
 //getParameters([‘tn‘,‘wd‘]);

么么哒。

原文地址:https://blog.51cto.com/13496570/2416775

时间: 2024-08-01 02:19:42

js 批量获取浏览器参数的相关文章

关于获取浏览器参数的见解

插件,jquery.query.js /** * jQuery.query - Query String Modification and Creation for jQuery * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com) * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/). * Date: 2009/8/13 * * @author Bl

JS中获取URL参数

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="/script/jquery-1.6.

关于JS中获取浏览器高度和宽度值的多种方法(多浏览器)

三种浏览器获取值方法 IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.documentElement.clientWidth ==> 可见区域宽度 document.documentElement.clientHeight ==> 可见区域高度 FireFox中: document.body.clientWidth ==> BODY对象宽度

在js中获取get参数(仿PHP)

复制粘贴即可 /*--------------------(返回 $_GET 对象, 仿PHP模式)----------------------*/ var $_GET = (function(){ var url = window.document.location.href.toString(); var u = url.split("?"); if(typeof(u[1]) == "string"){ u = u[1].split("&&qu

JS实时获取浏览器窗口尺寸 .

给div实时设置宽度 <div id="detail" style="width: 100%; overflow: scroll;"> </div> <script type="text/javascript">    var winWidth = 0;    var winHeight = 0;    function findDimensions() //函数:获取尺寸    {        //获取窗口

js如何获取URl参数的值

代码如下: var id = null; var name = null; if (window.location.search.indexOf("&") == -1){//如果不存在"&" id=window.location.search.substring(window.location.search.indexOf("=")+1); }else{ var parms = window.location.search.sub

js jq获取浏览器各种高度

document.body.clientWidth; //网页可见区域宽(body) document.body.clientHeight; //网页可见区域高(body) document.body.offsetWidth; //网页可见区域宽(body),包括border.margin等 document.body.offsetHeight; //网页可见区域宽(body),包括border.margin等 document.body.scrollWidth; //网页正文全文宽,包括有滚动

js 如何获取浏览器的高度?

<SCRIPT LANGUAGE="JavaScript"><!--var s = ""; s += " 网页可见区域宽:"+ document.body.clientWidth; s += " 网页可见区域高:"+ document.body.clientHeight; s += " 网页可见区域宽:"+ document.body.offsetWidth + " (包括边线和

JS获取浏览器高度 并赋值给类

在给网站做轮播焦点图的时候,如果需要全屏的话,可以用下面的JS来获取浏览器高度,然后赋值给类. $(window).load(function () { var maxHeight = 0; if (document.documentElement.clientHeight > maxHeight) { maxHeight = document.documentElement.clientHeight; } $('.you-class').css("height", maxHei