Click here to Skip to main content
Click here to Skip to main content

JScript Querystringer

, 29 Jan 2002
Rate this:
Please Sign up or sign in to vote.
Here's some simple JScript that returns the value part of a name-value querystring pair from inside an HTML page.
<!-- Add the rest of your HTML here -->

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

About the Author

SimonS
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 PinsussAnonymous1-Apr-05 2:39 
General%26 --&gt; &amp; PinsussJMS_5-Jun-04 6:04 
GeneralRe: %26 --&gt; &amp; PinmemberSimonS5-Jun-04 7:32 
GeneralWindows cannot find the file... PinmemberHawkSoftware25-Mar-02 13:51 
GeneralRe: Windows cannot find the file... Pinmembersimons25-Mar-02 20:29 
GeneralRe: Windows cannot find the file... PinmemberAnonymous23-Apr-02 20:22 
GeneralRe: Windows cannot find the file... Pinmemberscunn29-May-02 6:41 
GeneralPlease enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel30-Jan-02 21:30 
GeneralRe: Please enhance for passing parameters using bookmark notation Pinmembersimons31-Jan-02 1:17 
GeneralRe: Please enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel31-Jan-02 2:52 
GeneralRe: Please enhance for passing parameters using bookmark notation Pinmembersimons31-Jan-02 3:00 
GeneralRe: Please enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel31-Jan-02 3:40 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 30 Jan 2002
Article Copyright 2002 by SimonS
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid