php字符串search,js获取location.search每个查询字符串的值
形如https://www.debug.org/temp/test2.html?a=1&b=2#ddd這樣的鏈接,雖可通過location.search屬性獲取到問號后的所有查詢字符串值,但要想逐個訪問其中的每個查詢字符串的值,即參數a,b各自有什么值的話,得自己添加函數實現了,實現方法 如下:
function getQueryStringArgs () {
// 獲取查詢字符串參數,去除該字符串問號開關符號
var qs=location.search.length>0?location.search.substring(1):"",
args={},//存放所有查詢字符串參數對
items=qs.split("&"),
len=items.length,
name=null,
value=null;
if(qs.length==0) {
alert("沒有查詢字符串,提前退行!");
return;
};
for(var i=0;i
item=items[i].split("=");
name=decodeURIComponent(item[0]);
value=decodeURIComponent(item[1]);
args[name]=value;
}
return args;
}
// 使用這個函數示例,當前測試url是:https://www.debug.org/temp/test2.html?a=1&b=2#ddd
var qstr=getQueryStringArgs();
// var temp="";
// for(var i in qstr){
// temp+=(i+":"+qstr[i]+"\n");
// }
// alert(temp);
alert(qstr["b"]);
// alert(qstr.b);
提示:你可以先修改部分代碼再運行。
很顯示,這里定義了getQueryStringArgs()函數來實現,該函數返回args這一對象字面量,源碼中也有示例,演示怎么使用該函數的返回值!
總結
以上是生活随笔為你收集整理的php字符串search,js获取location.search每个查询字符串的值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据监测驱动下的信息流广告优化
- 下一篇: 常用css属性集(持续更新…)