Click here to Skip to main content
6,597,576 members and growing! (18,494 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Beginner

JScript Querystringer

By SimonS

Here's some simple JScript that returns the value part of a name-value querystring pair from inside an HTML page.
C++, JavascriptWin2K, WinXP, Visual Studio, MFC, Dev
Posted:29 Jan 2002
Views:67,148
Bookmarked:10 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 2.52 Rating: 3.60 out of 5

1

2

3

4
1 vote, 100.0%
5

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


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
My blog : BrokenKeyboards.com


Besides my day job as co-director of a successful software development company in Johannesburg, South Africa, I run a small company (Broken Keyboards Software) that has developed a system for managing exercise studios.


It is a relatively new development - initial work was started in 2003 using C# 1.0. Now using C# 1.1 with Access XP as a database. So the system represents the most current techniques and technologies as supported by Microsoft.
Performance is excellent with one customer currently having over 3500 students on record and almost 2 years worth of classes and signins.
If anyone is interested in seeing a demo or screenshots, please email.


My full CV can be download here in PDF format.

Occupation: Web Developer
Location: South Africa South Africa

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralExcelent Simon PinsussAnonymous3:39 1 Apr '05  
General%26 --> & PinsussJMS_7:04 5 Jun '04  
GeneralRe: %26 --> & PinmemberSimonS8:32 5 Jun '04  
GeneralWindows cannot find the file... PinmemberHawkSoftware14:51 25 Mar '02  
GeneralRe: Windows cannot find the file... Pinmembersimons21:29 25 Mar '02  
GeneralRe: Windows cannot find the file... PinmemberAnonymous21:22 23 Apr '02  
GeneralRe: Windows cannot find the file... Pinmemberscunn7:41 29 May '02  
GeneralPlease enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel22:30 30 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notation Pinmembersimons2:17 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel3:52 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notation Pinmembersimons4:00 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notation PinmemberVictor Vogelpoel4:40 31 Jan '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Jan 2002
Editor: Chris Maunder
Copyright 2002 by SimonS
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project