/** Powered By CMSDream Copyright © 2007-2008 All rights reserved. 15:10 2008-6-7 **/ function querystring(paramstring){ this.url=window.location.href; this.params = new Object(); this.initialize = function(){ if(paramstring.length == 0) return; if(paramstring.substring(0,1)=='?'){ paramstring=paramstring.substring(1); } paramstring = paramstring.replace(/\+/g, ' '); var args = paramstring.split('&'); for (var i=0;i<args.length;i++){ var value; var pair = args[i].split('='); var name = unescape(pair[0]); if (pair.length == 2) value = unescape(pair[1]); else value = name; this.params[name] = value; } }; this.get = function(key, defvalue){ return this.params[key]==null?defvalue:this.params[key]; }; this.setkey = function(key,value){ var fragPos = this.url.lastIndexOf("#"); var fragment = ""; if(fragPos > -1){ fragment = this.url.substring(fragPos); this.url = this.url.substring(0,fragPos); } var querystart = this.url.indexOf("?"); if(querystart<0){ this.url +="?"+key+"="+value; }else if(querystart==this.url.length-1){ this.url +=key+"="+value; }else{ var Re = new RegExp(key+"=[^\\s&#]*","gi"); if (Re.test(this.url)){ this.url=this.url.replace(Re,key+"="+value); }else{ this.url += "&"+key+"="+value; } } return this.url; }; this.initialize(); }
//假设网址为:http://www.a.com/aa.htm?a=value1&b=value2 var q = new qerystring(location.search); //如果取不到值用第二个参数作为默认值。 var a = q.get('a','defaultvalue'); var b = q.get('b','defaultvalue'); /* 运行结果: a=value1 b=value2 */ /*********************************** 如果要将网址插入一个参数 如:http://www.a.com/aa.htm?a=value1&b=value2&page=3 ************************************/ var q = new qerystring(location.search); var url = q.setkey('page','3'); //url 是所得的结果
阅读:176
评论:0
゛. - 看過世间最冷漠滴眼神, 爱過⒈生最无缘滴人﹎
该页面还没有任何评论,赶快占个沙发吧!