JavaScript 获取链接参数的方法: 1, 使用 getRequest() 方法获取链接参数. 2, 使用正则表达式 RegExp("(^|&)"+name+"=([^&]*)(&|$)","i") 获取链接参数等.
使用 getRequest() 方法获取链接参数:
- getRequest : function() {
- var url = location.search; // 获取 url 中 "?" 符后的字串
- var theRequest = new Object();
- if (url.indexOf("?") != -1) {
- var str = url.substr(1);
- strs = str.split("&");
- for(var i = 0; i < strs.length; i ++) {
- theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); }
- }
- return theRequest;
- }
JavaScript 获取链接参数值的方法:
示例: 获取 http://passport.csdn.net/account/login?from=http://write.blog.csdn.net/postedit 中 form 的参数值.
方法一: 通过正则表达式获取
- function getQueryString(name) {
- var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
- var r = Windows.location.search.substr(1).match(reg);
- if (r != null) return unescape(r[2]);
- return null;
- }
- var from = getQueryString("from");
- alert(from);
方法二: 通过切串放进数组的方式
- function GetRequest() {
- var url = location.search; // 获取 url 中 "?" 符后的字串
- var theRequest = new Object();
- if (url.indexOf("?") != -1) {
- var str = url.substr(1);
- strs = str.split("&");
- for(var i = 0; i < strs.length; i ++) {
- theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
- }
- }
- return theRequest;
- }
- var req = GetRequest();
- var from = req['from'];
- alert(from);
来源: http://www.css88.com/qa/javascript/11878.html