JS采用正则表达式简单获取URL地址栏参数

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

 

param:传入参数名字

例:

var docId = "101";

var randnum = Math.random();

url="../page/jtformat?docId="+docId+"&randnum="+randnum;

this.GetUrlParam(docId)返回101;

this.GetUrlParam(randnum)返回随机数randnum;

你可能感兴趣的:(js,地址栏参数获取)