Important alert: (current site time 7/15/2013 1:28:13 PM EDT)
 

VB icon

Auto-parse any search string from a URL

Email
Submitted on: 8/5/2003 2:52:29 AM
By: Martin Budden 
Level: Beginner
User Rating: By 2 Users
Compatibility: JavaScript
Views: 8022
author picture
(About the author)
 
     This JavaScript will parse any search string (the bit in the URL to the right of "?") and automatically declare all search parameters as JavaScript variables with values. It handles both numeric and string variable types correctly.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Auto-parse any search string from a URL
' Description:This JavaScript will parse any search string (the bit in the URL to the right of "?") and automatically declare all search parameters as JavaScript variables with values. It handles both numeric and string variable types correctly.
' By: Martin Budden
'
' Inputs:Any URL with a search string
'
' Returns:declares JavaScript variables with values
'
' Assumes:Nothing: just paste it in, it will work!
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3791&lngWId=14'for details.'**************************************

// This JavaScript will parse
// any search string
// and automatically declare
// all search parameters 
// as JavaScript variables
// with values.
// Feel free to use/abuse
// it any way you like.
// 
// Get the search string
// (without the leading "?"):
if (location.search.length > 0) {
launchString = location.search.substring(1, location.search.length);
// Split the string at each "&"
// and put the parts
// (name-value pairs) into an array:
var launchStringArray = launchString.split("&");
// Loop through each part in turn:
for (var i = 0; i <= launchStringArray.length - 1; i++) {
 // Get the left and right side
 // of each part (name-value pair):
 var left = launchStringArray[i].substring(0, launchStringArray[i].indexOf("="));
 var right = launchStringArray[i].substring(launchStringArray[i].indexOf("=") + 1, launchString.length);
 // If the right hand side
 // (value) is not numeric, give
 // it quotes to make it a string:
 if (isNaN(right)) {
 right = '"' + right + '"';
 }
 // Use the JavaScript eval()
 // function to declare
 // each part (name-value pair)
 // as a JavaScript variable
 // with a value assigned:
 eval("var " + left + " = " + right);
}
}


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/6/2003 11:09:06 AMdp_pro

This looked very useful, so I made this version that also accounts for special characters and for null values: if (location.search.length > 1) { // remove initial ? mark and split each param into an array var aParams = location.search.substring(1, location.search.length).split("&"); for (var i=0; i<=aParams.length-1; i++) { var left = aParams[i].substring(0, aParams[i].indexOf("=")); var right = aParams[i].substring(aParams[i].indexOf("=") + 1, aParams[i].length); if (isNaN(right)) right = '"' + right + '"'; if (right!="") right = " = " + unescape(right); //document.write("var " + unescape(left) + right + ";
"); eval("var " + unescape(left) + right); } }
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.