JS获取网页地址中的参数
                                                        
                                
                                6926次阅读
                            
                            
                                
                                594人点赞
                            
                            
                            
                                
                                发布时间: 2022-09-14 14:11:31
                            
                        
                        
                            
                                
                                扫码到手机查看
                                
                                    ![]() 
                                
                            
                        
                    代码借鉴《JS高级程序设计第三版》P207
function getQueryStringArgs(){
	//get query string without the initial ?
	var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
	
		//object to hold data
		args = {},
	
		//get individual items
		items = qs.length ? qs.split("&") : [],
		item = null,
		name = null,
		value = null,
		
		//used in for loop
		i = 0,
		len = items.length;
	
	//assign each item onto the args object
	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;
}应用:
//assume query string of ?q=javascript&num=10
var args = getQueryStringArgs();
alert(args["q"]);     //"javascript"
alert(args["num"]);   //"10" 
                     
                     
                     
                     
                     
                     
                     
             
             
             目录
        目录