javascriptはURLの中のパラメータを獲得します.

896 ワード

<script>
function getConditionFromUrl(url,key){
	var position = url.indexOf('?');
	if(position > 0){
		var tempUrl = url.substring(position + 1);
		var arr = tempUrl.split('&');
		for(var i = 0, j = arr.length; i < j; i++){
			var tempArr = arr[i].split('=');
			if(key === tempArr[0]){
				return tempArr[1];
			}
		}
	}
	return '';	
}

var url = 'http://www.baidu.com/seach?name=qudanna&age=28&sex=1&address=Beijing';
alert(getConditionFromUrl(url,'name'));
alert(getConditionFromUrl(url,'age'));
alert(getConditionFromUrl(url,'sex'));
alert(getConditionFromUrl(url,'address'));
alert(getConditionFromUrl(url,'haha'));

</script>