Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

JScript Querystringer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
29 Jan 2002 89K   11   12
Here's some simple JScript that returns the value part of a name-value querystring pair from inside an HTML page.

Introduction

One of the things that can get overlooked in web development is the fact that you can pass parameters to HTML pages without having to bounce a redirect statement off the server. This can be pretty useful if the HTML page is a generic that loads various xml files based on that parameter (like a product details page with a productID being passed). Here's the code:

function retVal(sName)
{

  /*
   get last loc. of ?
   right: find first loc. of sName
   +2
   retrieve value before next &
  
  */
  
  var sURL = new String(window.location);
  var iQMark= sURL.lastIndexOf('?');
  var iLensName=sName.length;
  
  //retrieve loc. of sName
  var iStart = sURL.indexOf('?' + sName +'=') //limitation 1
  if (iStart==-1)
        {//not found at start
        iStart = sURL.indexOf('&' + sName +'=')//limitation 1
		if (iStart==-1)
		   {//not found at end
		    return 0; //not found
		   }   
        }
        
  iStart = iStart + + iLensName + 2;
  var iTemp= sURL.indexOf('&',iStart); //next pair start
  if (iTemp ==-1)
		{//EOF
		iTemp=sURL.length;
		}  
  return sURL.slice(iStart,iTemp ) ;
  sURL=null;//destroy String
}


alert( retVal('mynewname'));

Usage:

Save the code into a normal HTML page and call it like this:

mypage.htm?mynewname=testvalue

and "testvalue" should be returned in the alert box. The "mynewname" is just a literal and can be replaced with any querystring name that has a value related to it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
* Visual C# MVP 2004, 2005 - South Africa
* SADeveloper.NET User Group co-founder and lead 2003, 2004, 2005

MSN : simon_stewart AT hotmail.com
Email : simon AT brokenkeyboards.com
Skype: brokenkeyboards
CEO of Broken Keyboards Software



Founder of these startups:


Browse This For Me


Monitor My URL



My full CV can be download here in PDF format.

Comments and Discussions

 
GeneralExcelent Simon Pin
Anonymous1-Apr-05 2:39
Anonymous1-Apr-05 2:39 
General%26 --> & Pin
JMS_5-Jun-04 6:04
sussJMS_5-Jun-04 6:04 
GeneralRe: %26 --> & Pin
SimonS5-Jun-04 7:32
SimonS5-Jun-04 7:32 
GeneralWindows cannot find the file... Pin
HawkSoftware25-Mar-02 13:51
HawkSoftware25-Mar-02 13:51 
GeneralRe: Windows cannot find the file... Pin
SimonS25-Mar-02 20:29
SimonS25-Mar-02 20:29 
GeneralRe: Windows cannot find the file... Pin
23-Apr-02 20:22
suss23-Apr-02 20:22 
GeneralRe: Windows cannot find the file... Pin
29-May-02 6:41
suss29-May-02 6:41 
GeneralPlease enhance for passing parameters using bookmark notation Pin
Victor Vogelpoel30-Jan-02 21:30
Victor Vogelpoel30-Jan-02 21:30 
GeneralRe: Please enhance for passing parameters using bookmark notation Pin
SimonS31-Jan-02 1:17
SimonS31-Jan-02 1:17 
GeneralRe: Please enhance for passing parameters using bookmark notation Pin
Victor Vogelpoel31-Jan-02 2:52
Victor Vogelpoel31-Jan-02 2:52 
GeneralRe: Please enhance for passing parameters using bookmark notation Pin
SimonS31-Jan-02 3:00
SimonS31-Jan-02 3:00 
GeneralRe: Please enhance for passing parameters using bookmark notation Pin
Victor Vogelpoel31-Jan-02 3:40
Victor Vogelpoel31-Jan-02 3:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.