5,696,038 members and growing! (12,303 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, Windows, Visual Studio, MFC, Dev

Posted: 29 Jan 2002
Updated: 29 Jan 2002
Views: 60,124
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.52 Rating: 3.60 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
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


* 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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralExcelent SimonsussAnonymous3:39 1 Apr '05  
General%26 --> &sussJMS_7:04 5 Jun '04  
GeneralRe: %26 --> &memberSimonS8:32 5 Jun '04  
GeneralWindows cannot find the file...memberHawkSoftware14:51 25 Mar '02  
GeneralRe: Windows cannot find the file...membersimons21:29 25 Mar '02  
GeneralRe: Windows cannot find the file...memberAnonymous21:22 23 Apr '02  
GeneralRe: Windows cannot find the file...memberscunn7:41 29 May '02  
GeneralPlease enhance for passing parameters using bookmark notationmemberVictor Vogelpoel22:30 30 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notationmembersimons2:17 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notationmemberVictor Vogelpoel3:52 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notationmembersimons4:00 31 Jan '02  
GeneralRe: Please enhance for passing parameters using bookmark notationmemberVictor 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-2008
Web08 | Advertise on the Code Project