Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a function that Pulls info from Querystring 1

Status
Not open for further replies.

deharris2003

Programmer
Jul 1, 2003
41
US
Is it possible to get data from the querystring in Javascript?

Something like

if (<%Request.Querystring(&quot;ID&quot;)%> == &quot;99999&quot;)
{
document.getElementsByTagName('DIV')[0].style.visibility = &quot;Visible&quot;;
else
document.getElementsByTagName('DIV')[0].style.visibility = &quot;Hidden&quot;;
}
Any help is appreciated I am barlely learning Javascript However I am more familiar with ASP
 
The browser has an object called 'location'. The location object has a number of properties, one of which is 'search'. The search property returns whatever information is after (and including) the question mark '?' in the current URL - the Query_String in other words.

So if you were at the url [ignore][/ignore] then location.search would return:
?state=VIC&country=AU&planet=Earth

To strip out the question mark we can use the JavaScript slice function - location.search.slice(1, location.search.length) would return:
state=VIC&country=AU&planet=Earth

To split that up into field-value pairs we can use the JavaScript split() function. The code sample below demonstrates:
Code:
function splitsearch(){
 //get the search string
 // var searchstring = location.search; // uncomment this line to test for real
 var searchstring = &quot;?state=VIC&country=AU&planet=Earth&quot;; // comment this line to test for real
 //strip off the ?
 searchstring = searchstring.slice(1,searchstring.length);
 //split it up into an array
 var stringArray = searchstring.split('&');
 //declare a new array to hold the split pairs
 var pairsArray = new Array()
 //loop through the stringArray and split each pair
 for(var count = 0; count < stringArray.length; count++){
  pairsArray[count] = stringArray[count].split('=');
 }
 //we can now access each field and it's corresponding value through the arrays
 alert(pairsArray[0][0] + ':\t' + pairsArray[0][1] + '\n' +
       pairsArray[1][0] + ':\t' + pairsArray[1][1] + '\n' +
	   pairsArray[2][0] + ':\t' + pairsArray[2][1] + '\n');
 
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top