获取url参数
代码如下 |
复制代码 |
<script language = javascript>
function request(paras){
var url = location.href;
var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
var paraObj = {}
for (i=0; j=paraString[i]; i++){
paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
}
var returnValue = paraObj[paras.toLowerCase()];
if(typeof(returnValue)=="undefined"){
return "";
}else{
return returnValue;
}
}
var theurl=request('url');
var theimg=request('img');
document.writeln("<a href='" + theurl + "' target=_blank><img src=" + theimg + " width=750 height=450 border=0></a>");
</script>
|
核心代码是
代码如下 |
复制代码 |
function request(paras){
var url = location.href;
var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
var paraObj = {}
for (i=0; j=paraString[i]; i++){
paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
}
var returnValue = paraObj[paras.toLowerCase()];
if(typeof(returnValue)=="undefined"){
return "";
}else{
return returnValue;
}
}
|
js 设置url参数
代码如下 |
复制代码 |
// 获取地址栏的参数数组
function getUrlParams() {
var search = window.location.search;
// 写入数据字典
var tmparray = search.substr(1, search.length).split("&");
var paramsArray = new Array;
if (tmparray != null) {
for (var i = 0; i < tmparray.length; i++) {
var reg = /[=|^==]/; // 用=进行拆分,但不包括==
var set1 = tmparray[i].replace(reg, '&');
var tmpStr2 = set1.split('&');
var array = new Array;
array[tmpStr2[0]] = tmpStr2[1];
paramsArray.push(array);
}
}
// 将参数数组进行返回
return paramsArray;
}
// 根据参数名称获取参数值
function getParamValue(name) {
var paramsArray = getUrlParams();
if (paramsArray != null) {
for (var i = 0; i < paramsArray.length; i++) {
for (var j in paramsArray[i]) {
if (j == name) {
return paramsArray[i][j];
}
}
}
}
return null;
} |
js修改url参数
代码如下 |
复制代码 |
function changeURLPar(url, ref, value) {
var str = "";
if (url.indexOf('?') != -1)
str = url.substr(url.indexOf('?') + 1);
else
return url + "?" + ref + "=" + value;
var returnurl = "";
var setparam = "";
var arr;
var modify = "0";
if (str.indexOf('&') != -1) {
arr = str.split('&');
for (i in arr) {
if (arr[i].split('=')[0] == ref) {
setparam = value;
modify = "1";
}
else {
setparam = arr[i].split('=')[1];
}
returnurl = returnurl + arr[i].split('=')[0] + "=" + setparam + "&";
}
returnurl = returnurl.substr(0, returnurl.length - 1);
if (modify == "0")
if (returnurl == str)
returnurl = returnurl + "&" + ref + "=" + value;
}
else {
if (str.indexOf('=') != -1) {
arr = str.split('=');
if (arr[0] == ref) {
setparam = value;
modify = "1";
}
else {
setparam = arr[1];
}
returnurl = arr[0] + "=" + setparam;
if (modify == "0")
if (returnurl == str)
returnurl = returnurl + "&" + ref + "=" + value;
}
else
returnurl = ref + "=" + value;
}
return url.substr(0, url.indexOf('?')) + "?" + returnurl;
}
|
js解析url参数
代码如下 |
复制代码 |
<script type="text/javascript">
function getQueryString(url) {
if(url) {
url=url.substr(url.indexOf("?")+1); //字符串截取,比我之前的split()方法效率高
}
var result = {}, //创建一个对象,用于存name,和value
queryString =url || location.search.substring(1), //location.search设置或返回从问号 (?) 开始的 URL(查询部分)。
re = /([^&=]+)=([^&]*)/g, //正则,具体不会用
m;
while (m = re.exec(queryString)) { //exec()正则表达式的匹配,具体不会用
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]); //使用 decodeURIComponent() 对编码后的 URI 进行解码
}
return result;
}
// demo
var myParam = getQueryString("www.taobao.com?key0=a&key1=b&key2=c");
alert(myParam.key1);
</script>
|
|