求教js获取url参数 50
现在我有一个url,它的格式是这样的:http://192.168.2.1/bulletin_0.htm?r=0|url=【此处是需要获取的网址】|ip=192.168....
现在我有一个url,它的格式是这样的:
http://192.168.2.1/bulletin_0.htm?r=0|url=【此处是需要获取的网址】|ip=192.168.2.2
有一个地址是(如上所示),现在想把“此处是需要获取的网址”部分的url都获取下来,并且把刚刚获取到的那个网址参数,赋给另一个网址http://192.168.1.1/tgfile.htm?fn=0&__ld=此处是需要被赋的参数,并转到这个网址。
----------
求教全部js代码(从一开始在地址栏获取代码然后拆分截取一直到最后赋参数给新地址并转到这个地址的全过程代码)谢谢,谢谢大神们了!!!小弟真的对网页程序不太懂,所以还望写的详细一点,只有50金币了全拿出来了。
------------------
举个例子,比如我现在的地址是http://192.168.2.1/bulletin_0.htm?r=0|url=www.a.com|ip=192.168.2.2,那么我想把www.a.com截取下来,然后赋给另一个地址址的尾部,http://192.168.1.1/tgfile.htm?fn=0&__ld=www.a.com,并当场转到http://192.168.1.1/tgfile.htm?fn=0&__ld=www.a.com这个网址。
还有要注意一点就是“需要获取的地址”不光是主域名形式,还有可能是子链形式,比如“www.a.com/b.html”这类形式,甚至有可能是某宝连接的那种带“&?=”的形式,所以如果需要划分组的话,可能需要考虑更多因素,所以还望各位大神考虑周全一些,小弟拜托拜托各位了!为了感谢大家,如果回答的好,小弟可以想办法多赚点财富然后追加上去。 展开
http://192.168.2.1/bulletin_0.htm?r=0|url=【此处是需要获取的网址】|ip=192.168.2.2
有一个地址是(如上所示),现在想把“此处是需要获取的网址”部分的url都获取下来,并且把刚刚获取到的那个网址参数,赋给另一个网址http://192.168.1.1/tgfile.htm?fn=0&__ld=此处是需要被赋的参数,并转到这个网址。
----------
求教全部js代码(从一开始在地址栏获取代码然后拆分截取一直到最后赋参数给新地址并转到这个地址的全过程代码)谢谢,谢谢大神们了!!!小弟真的对网页程序不太懂,所以还望写的详细一点,只有50金币了全拿出来了。
------------------
举个例子,比如我现在的地址是http://192.168.2.1/bulletin_0.htm?r=0|url=www.a.com|ip=192.168.2.2,那么我想把www.a.com截取下来,然后赋给另一个地址址的尾部,http://192.168.1.1/tgfile.htm?fn=0&__ld=www.a.com,并当场转到http://192.168.1.1/tgfile.htm?fn=0&__ld=www.a.com这个网址。
还有要注意一点就是“需要获取的地址”不光是主域名形式,还有可能是子链形式,比如“www.a.com/b.html”这类形式,甚至有可能是某宝连接的那种带“&?=”的形式,所以如果需要划分组的话,可能需要考虑更多因素,所以还望各位大神考虑周全一些,小弟拜托拜托各位了!为了感谢大家,如果回答的好,小弟可以想办法多赚点财富然后追加上去。 展开
2个回答
展开全部
推荐你使用一个专门处理这种情况的jquery插件,目前网上已经很难下载到,我就把源码贴这里,你自己保存到一个js中,然后在页面中引用该js。使用方法在注释中,本人亲自使用过,才会在这里给你回答。请采纳。
/**
//例如 当前你的URL是:
//http://www.jb51.net/index.php?test=1&kk=2
//
//如果想获取test,则可以引入插件后,
//用如下方法获取:
//var test = $.query.get('test');
//
//如果参数有多个相同的名称 ,则可以这样:
//var arr = $.query.get('testy');
//输出: [ 值1 , 值2, 值3...]
//
//如果要获取多个相同名称中的某一个,可以这样:
//var arrayElement = $.query.get('testy[1]');
//
//此插件不仅能获取参数,也能设置参数。
//
//设置一个参数:
//var newUrl = $.query.set("section", 5).toString();
//输出 : "?section=5"
//
//设置两个参数:
//var newUrl = $.query.set("section", 5).set("action", "do").toString();
//输出 : "?action=do§ion=5"
//
//
//删除一个参数:
//var oldQueryAgain = $.query.REMOVE("type");
//
//清空所有参数:
//var emptyQuery = $.query.empty();
//
//复制所有参数:
//var stillTheSame = $.query.copy();
new function(settings) {
// Various Settings
var $separator = settings.separator || '&';
var $spaces = settings.spaces === false ? false : true;
var $suffix = settings.suffix === false ? '' : '[]';
var $prefix = settings.prefix === false ? false : true;
var $hash = $prefix ? settings.hash === true ? "#" : "?" : "";
var $numbers = settings.numbers === false ? false : true;
jQuery.query = new function() {
var is = function(o, t) {
return o != undefined && o !== null && (!!t ? o.constructor == t : true);
};
var parse = function(path) {
var m, rx = /\[([^[]*)\]/g , match = /^([^[]+)(\[.*\])?$/ .exec(path), base = match[1], tokens = [];
while (m = rx.exec(match[2])) tokens.push(m[1]);
return [base, tokens];
};
var set = function(target, tokens, value) {
var o, token = tokens.shift();
if (typeof target != 'object') target = null;
if (token === "") {
if (!target) target = [];
if (is(target, Array)) {
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
} else if (is(target, Object)) {
var i = 0;
while (target[i++] != null) ;
target[--i] = tokens.length == 0 ? value : set(target[i], tokens.slice(0), value);
} else {
target = [];
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
}
} else if (token && token.match( /^\s*[0-9]+\s*$/ )) {
var index = parseInt(token, 10);
if (!target) target = [];
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
} else if (token) {
var index = token.replace( /^\s*|\s*$/g , "");
if (!target) target = { };
if (is(target, Array)) {
var temp = { };
for (var i = 0; i < target.length; ++i) {
temp[i] = target[i];
}
target = temp;
}
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
} else {
return value;
}
return target;
};
var queryObject = function(a) {
var self = this;
self.keys = { };
if (a.queryObject) {
jQuery.each(a.get(), function(key, val) {
self.SET(key, val);
});
} else {
jQuery.each(arguments, function() {
var q = "" + this;
q = q.replace( /^[?#]/ , ''); // remove any leading ? || #
q = q.replace( /[;&]$/ , ''); // remove any trailing & || ;
if ($spaces) q = q.replace( /[+]/g , ' '); // replace +'s with spaces
jQuery.each(q.split( /[&;]/ ), function() {
var key = decodeURIComponent(this.split('=')[0] || "");
var val = decodeURIComponent(this.split('=')[1] || "");
if (!key) return;
if ($numbers) {
if ( /^[+-]?[0-9]+\.[0-9]*$/ .test(val)) // simple float regex
val = parseFloat(val);
else if ( /^[+-]?[0-9]+$/ .test(val)) // simple int regex
val = parseInt(val, 10);
}
val = (!val && val !== 0) ? true : val;
if (val !== false && val !== true && typeof val != 'number')
val = val;
self.SET(key, val);
});
});
}
return self;
};
queryObject.prototype = {
queryObject: true,
has: function(key, type) {
var value = this.get(key);
return is(value, type);
},
GET: function(key) {
if (!is(key)) return this.keys;
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
var target = this.keys[base];
while (target != null && tokens.length != 0) {
target = target[tokens.shift()];
}
return typeof target == 'number' ? target : target || "";
},
get: function(key) {
var target = this.GET(key);
if (is(target, Object))
return jQuery.extend(true, { }, target);
else if (is(target, Array))
return target.slice(0);
return target;
},
SET: function(key, val) {
var value = !is(val) ? null : val;
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
var target = this.keys[base];
this.keys[base] = set(target, tokens.slice(0), value);
return this;
},
set: function(key, val) {
return this.copy().SET(key, val);
},
REMOVE: function(key) {
return this.SET(key, null).COMPACT();
},
remove: function(key) {
return this.copy().REMOVE(key);
},
EMPTY: function() {
var self = this;
jQuery.each(self.keys, function(key, value) {
delete self.keys[key];
});
return self;
},
load: function(url) {
var hash = url.replace( /^.*?[#](.+?)(?:\?.+)?$/ , "$1");
var search = url.replace( /^.*?[?](.+?)(?:#.+)?$/ , "$1");
return new queryObject(url.length == search.length ? '' : search, url.length == hash.length ? '' : hash);
},
empty: function() {
return this.copy().EMPTY();
},
copy: function() {
return new queryObject(this);
},
COMPACT: function() {
function build(orig) {
var obj = typeof orig == "object" ? is(orig, Array) ? [] : { } : orig;
if (typeof orig == 'object') {
function add(o, key, value) {
if (is(o, Array))
o.push(value);
else
o[key] = value;
}
jQuery.each(orig, function(key, value) {
if (!is(value)) return true;
add(obj, key, build(value));
});
}
return obj;
}
this.keys = build(this.keys);
return this;
},
compact: function() {
return this.copy().COMPACT();
},
toString: function() {
var i = 0, queryString = [], chunks = [], self = this;
var encode = function(str) {
str = str + "";
if ($spaces) str = str.replace( / /g , "+");
return encodeURIComponent(str);
};
var addFields = function(arr, key, value) {
if (!is(value) || value === false) return;
var o = [encode(key)];
if (value !== true) {
o.push("=");
o.push(encode(value));
}
arr.push(o.join(""));
};
var build = function(obj, base) {
var newKey = function(key) {
return !base || base == "" ? [key].join("") : [base, "[", key, "]"].join("");
};
jQuery.each(obj, function(key, value) {
if (typeof value == 'object')
build(value, newKey(key));
else
addFields(chunks, newKey(key), value);
});
};
build(this.keys);
if (chunks.length > 0) queryString.push($hash);
queryString.push(chunks.join($separator));
return queryString.join("");
}
};
return new queryObject(location.search, location.hash);
};
}(jQuery.query || { }); // Pass in jQuery.query as settings object
/**
//例如 当前你的URL是:
//http://www.jb51.net/index.php?test=1&kk=2
//
//如果想获取test,则可以引入插件后,
//用如下方法获取:
//var test = $.query.get('test');
//
//如果参数有多个相同的名称 ,则可以这样:
//var arr = $.query.get('testy');
//输出: [ 值1 , 值2, 值3...]
//
//如果要获取多个相同名称中的某一个,可以这样:
//var arrayElement = $.query.get('testy[1]');
//
//此插件不仅能获取参数,也能设置参数。
//
//设置一个参数:
//var newUrl = $.query.set("section", 5).toString();
//输出 : "?section=5"
//
//设置两个参数:
//var newUrl = $.query.set("section", 5).set("action", "do").toString();
//输出 : "?action=do§ion=5"
//
//
//删除一个参数:
//var oldQueryAgain = $.query.REMOVE("type");
//
//清空所有参数:
//var emptyQuery = $.query.empty();
//
//复制所有参数:
//var stillTheSame = $.query.copy();
new function(settings) {
// Various Settings
var $separator = settings.separator || '&';
var $spaces = settings.spaces === false ? false : true;
var $suffix = settings.suffix === false ? '' : '[]';
var $prefix = settings.prefix === false ? false : true;
var $hash = $prefix ? settings.hash === true ? "#" : "?" : "";
var $numbers = settings.numbers === false ? false : true;
jQuery.query = new function() {
var is = function(o, t) {
return o != undefined && o !== null && (!!t ? o.constructor == t : true);
};
var parse = function(path) {
var m, rx = /\[([^[]*)\]/g , match = /^([^[]+)(\[.*\])?$/ .exec(path), base = match[1], tokens = [];
while (m = rx.exec(match[2])) tokens.push(m[1]);
return [base, tokens];
};
var set = function(target, tokens, value) {
var o, token = tokens.shift();
if (typeof target != 'object') target = null;
if (token === "") {
if (!target) target = [];
if (is(target, Array)) {
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
} else if (is(target, Object)) {
var i = 0;
while (target[i++] != null) ;
target[--i] = tokens.length == 0 ? value : set(target[i], tokens.slice(0), value);
} else {
target = [];
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
}
} else if (token && token.match( /^\s*[0-9]+\s*$/ )) {
var index = parseInt(token, 10);
if (!target) target = [];
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
} else if (token) {
var index = token.replace( /^\s*|\s*$/g , "");
if (!target) target = { };
if (is(target, Array)) {
var temp = { };
for (var i = 0; i < target.length; ++i) {
temp[i] = target[i];
}
target = temp;
}
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
} else {
return value;
}
return target;
};
var queryObject = function(a) {
var self = this;
self.keys = { };
if (a.queryObject) {
jQuery.each(a.get(), function(key, val) {
self.SET(key, val);
});
} else {
jQuery.each(arguments, function() {
var q = "" + this;
q = q.replace( /^[?#]/ , ''); // remove any leading ? || #
q = q.replace( /[;&]$/ , ''); // remove any trailing & || ;
if ($spaces) q = q.replace( /[+]/g , ' '); // replace +'s with spaces
jQuery.each(q.split( /[&;]/ ), function() {
var key = decodeURIComponent(this.split('=')[0] || "");
var val = decodeURIComponent(this.split('=')[1] || "");
if (!key) return;
if ($numbers) {
if ( /^[+-]?[0-9]+\.[0-9]*$/ .test(val)) // simple float regex
val = parseFloat(val);
else if ( /^[+-]?[0-9]+$/ .test(val)) // simple int regex
val = parseInt(val, 10);
}
val = (!val && val !== 0) ? true : val;
if (val !== false && val !== true && typeof val != 'number')
val = val;
self.SET(key, val);
});
});
}
return self;
};
queryObject.prototype = {
queryObject: true,
has: function(key, type) {
var value = this.get(key);
return is(value, type);
},
GET: function(key) {
if (!is(key)) return this.keys;
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
var target = this.keys[base];
while (target != null && tokens.length != 0) {
target = target[tokens.shift()];
}
return typeof target == 'number' ? target : target || "";
},
get: function(key) {
var target = this.GET(key);
if (is(target, Object))
return jQuery.extend(true, { }, target);
else if (is(target, Array))
return target.slice(0);
return target;
},
SET: function(key, val) {
var value = !is(val) ? null : val;
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
var target = this.keys[base];
this.keys[base] = set(target, tokens.slice(0), value);
return this;
},
set: function(key, val) {
return this.copy().SET(key, val);
},
REMOVE: function(key) {
return this.SET(key, null).COMPACT();
},
remove: function(key) {
return this.copy().REMOVE(key);
},
EMPTY: function() {
var self = this;
jQuery.each(self.keys, function(key, value) {
delete self.keys[key];
});
return self;
},
load: function(url) {
var hash = url.replace( /^.*?[#](.+?)(?:\?.+)?$/ , "$1");
var search = url.replace( /^.*?[?](.+?)(?:#.+)?$/ , "$1");
return new queryObject(url.length == search.length ? '' : search, url.length == hash.length ? '' : hash);
},
empty: function() {
return this.copy().EMPTY();
},
copy: function() {
return new queryObject(this);
},
COMPACT: function() {
function build(orig) {
var obj = typeof orig == "object" ? is(orig, Array) ? [] : { } : orig;
if (typeof orig == 'object') {
function add(o, key, value) {
if (is(o, Array))
o.push(value);
else
o[key] = value;
}
jQuery.each(orig, function(key, value) {
if (!is(value)) return true;
add(obj, key, build(value));
});
}
return obj;
}
this.keys = build(this.keys);
return this;
},
compact: function() {
return this.copy().COMPACT();
},
toString: function() {
var i = 0, queryString = [], chunks = [], self = this;
var encode = function(str) {
str = str + "";
if ($spaces) str = str.replace( / /g , "+");
return encodeURIComponent(str);
};
var addFields = function(arr, key, value) {
if (!is(value) || value === false) return;
var o = [encode(key)];
if (value !== true) {
o.push("=");
o.push(encode(value));
}
arr.push(o.join(""));
};
var build = function(obj, base) {
var newKey = function(key) {
return !base || base == "" ? [key].join("") : [base, "[", key, "]"].join("");
};
jQuery.each(obj, function(key, value) {
if (typeof value == 'object')
build(value, newKey(key));
else
addFields(chunks, newKey(key), value);
});
};
build(this.keys);
if (chunks.length > 0) queryString.push($hash);
queryString.push(chunks.join($separator));
return queryString.join("");
}
};
return new queryObject(location.search, location.hash);
};
}(jQuery.query || { }); // Pass in jQuery.query as settings object
展开全部
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}
var myUrl = getURLParameter('url');
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}
var myUrl = getURLParameter('url');
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询