- /*
- *第一种方法:获取url的参数
- ***************************/
- 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"));
- /*
- *第二种方法:获取url的参数
- ***************************/
- UrlParse = function() {
- this.host = location.host;
- //this.GET = _GET();
- this.url = location.herf;
- this.hash = location.hash;
- this.port = location.port;
- this.script_name = location.pathname;
- this.quartyString = location.search;
- this.protocol = location.protocol;
- this.GET = this.parseArguement();
- }
- UrlParse.prototype = {
- parseArguement:function (){
- var pos;
- var arg = new Object();
- var quire = location.search.substring(1);
- var pair = quire.split("&");
- if(pair.length > 0)
- {
- for(var i=0;i < pair.length ;i++)
- {
- pos = pair[i].indexOf("=");
- if(pos == -1) continue;
- argName = pair[i].substring(0,pos);
- argValue = pair[i].substring(pos+1);
- arg[argName] = argValue;
- }
- }else
- {
- de = quire.indexOf("=");
- if(de != -1)
- {
- argName = pair[i].substring(0,de);
- argValue = pair[i].substring(de+1);
- arg[argName] = argValue;
- }
- }
- return arg;
- }
- }
- var Url = new UrlParse();
- alert(Url.GET);
- for(key in Url.GET)
- {
- document.write(key + " : " + Url.GET[key]);
- }
- //该片段来自于http://www.codesnippet.cn/detail/0909201513661.html
来源: http://www.codesnippet.cn/detail/0909201513661.html