Javascript仿PHP $_GET获取URL中的参数
代码如下:
/* 像PHP的 $_GET['arg'] 那样获得地址栏GET参数 */ function getArgs() { var args = {}; var query = location.search.substring(1); // Get query string var pairs = query.split("&"); // Break at ampersand for(var i = 0; i < pairs.length; i++) { var pos = pairs[i].indexOf('='); // Look for "name=value" if (pos == -1) continue; // If not found, skip var argname = pairs[i].substring(0,pos); // Extract the name var value = pairs[i].substring(pos+1); // Extract the value value = decodeURIComponent(value); // Decode it, if needed args[argname] = value; // Store as a property } return args; // Return the object } /* 使用方法 */ /* URL: http://www.baidu.com?user=funsion&age=26 */ alert( getArgs()['user'] ); // 输出 funsion alert( getArgs()['age'] ); // 输出 26
相关推荐
xuebingnan 2020-11-05
Crazyshark 2020-09-15
zrtlin 2020-11-09
wikiwater 2020-10-27
heheeheh 2020-10-19
softwear 2020-08-21
ZGCdemo 2020-08-16
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25
Aveiox 2020-06-23
dragonzht 2020-06-17