Click here to Skip to main content
Licence 
First Posted 29 Jan 2002
Views 74,018
Bookmarked 11 times

JScript Querystringer

By | 29 Jan 2002 | Article
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

About the Author

SimonS

Web Developer

South Africa South Africa

Member

* 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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralExcelent Simon PinsussAnonymous2:39 1 Apr '05  
General%26 --> & PinsussJMS_6:04 5 Jun '04  
GeneralRe: %26 --> & PinmemberSimonS7:32 5 Jun '04  
GeneralWindows cannot find the file... PinmemberHawkSoftware13:51 25 Mar '02  
GeneralRe: Windows cannot find the file... Pinmembersimons20:29 25 Mar '02  
GeneralRe: Windows cannot find the file... PinmemberAnonymous20:22 23 Apr '02  
GeneralRe: Windows cannot find the file... Pinmemberscunn6:41 29 May '02  
GeneralPlease enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel21:30 30 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notation Pinmembersimons1:17 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel2:52 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notation Pinmembersimons3:00 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel3:40 31 Jan '02  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 30 Jan 2002
Article Copyright 2002 by SimonS
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid