通过location对象的search属性截取字符串传递过来的参数

search ?item=hello&name=auto&age=25 返回url中传递的参数,以?开头

function getQueryStringArgs(){
	var qs=(location.search.length>0?location.search.substring(1):""); //去除?
	args={}, 
	items=qs.length?qs.split("&"):[];  
	item=null,
	name=null,
	value=null,
	i=0,
	len=items.length;
	for(i=0;i<len;i++){
		item=items[i].split("=");
		
		name=decodeURIComponent(item[0]);
		
		value=decodeURIComponent(item[1]);

		if (name.length) {
			args[name]=value;
		};
	}
	return args;


}
getQueryStringArgs();
alert(args.item);    

	</script>

  最后,每个字符串查询参数都成了,args对象的属性,value为属性值

相关文章:

  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
相关资源
相似解决方案