[JS]Change a searching options of a web page

2017-3-28 写技术

There is a function by myself to change a searching options of a web page.


function setName(name, val)
{
	var isSet = 0;
	var url=window.location.search;
	var search = "";
	if(url.length>200){
		return 0;
	}
	var url_domain = window.location.href;
	if(url_domain.indexOf("?")!=-1){
		url_domain = url_domain.substr(0, url_domain.indexOf("?"));
	}
	if(url.indexOf("?")!=-1){ 
		var str = url.substr(1);
		strs = str.split("&"); 
		var key=new Array(strs.length);
		var value=new Array(strs.length);
		for(i=0;i<strs.length;i++){ 
     			key[i]=strs[i].split("=")[0]
     			value[i]=unescape(strs[i].split("=")[1]); 

			if(key[i]==name){
				if(value[i] == val){
					return 0;
				}
				value[i] = val;
				isSet = 1;
			}

			if(search != ""){
				search += "&";
			}
			search += key[i]+"="+value[i];
		} 
	} 
	if(isSet == 0){
		if(search != ""){
			search += "&";
		}		
		search += name+"="+val;
	}
	window.location = url_domain + "?" + search;
}


标签: JS

发表评论:

Powered by anycle 湘ICP备15001973号-1