location对象,将url解析为独立片段search属性截取传递的参数

通过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为属性值

你可能感兴趣的:(location)