Jump to content

querystring parsing


fooDigi

Recommended Posts

i can parse a well formatted querystring just fine, the problem arises when a second '?' gets put towards the end, and i can not retrieve the variables after that. how would i do this.

 

the problem should be fixed at the source, by removing the second '?'. but i must be prepared for poorly formatted urls.

 

here is the function i use.

 

function getQueryString(q){
var fullS = window.location.search.substring(1);
if(fullS.indexOf(q) != -1){
	var varA = fullS.split('&');		
	for(i=0; i<=varA.length; i++){
		nameA = varA[i].split('=');	
		if(nameA[0] == q){
			retVal = nameA[1];
			break;
		}
	}
	if(retVal.length > 0)
		return retVal;
	else
		return false;
}else{
	return false;
}
}
document.write(getQueryString('qs'));

 

thx for any help

foo

Link to comment
https://forums.phpfreaks.com/topic/38538-querystring-parsing/
Share on other sites

i can parse a well formatted querystring just fine, the problem arises when a second '?' gets put towards the end, and i can not retrieve the variables after that. how would i do this.

 

the problem should be fixed at the source, by removing the second '?'. but i must be prepared for poorly formatted urls.

 

If all you want to do is remove the second ?, use a regular expression before any other processing of the search string.

 

here is the function i use.

 

function getQueryString(q){
var fullS = window.location.search.substring(1);

 

try:

 

var fullS = window.location.search.substring(1).replace(/\?/g,'');

 

 

but be careful: that will replace all instances of "?" in the query string.

 

 

Link to comment
https://forums.phpfreaks.com/topic/38538-querystring-parsing/#findComment-185010
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.