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

JScript Querystringer

By , 29 Jan 2002
 

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralExcelent SimonsussAnonymous1 Apr '05 - 2:39 
Well done.
General%26 --> &sussJMS_5 Jun '04 - 6:04 
The '&' may get mangled to hex (%26).
You may want to add a line like this after you get the URL to fix that:
sURL = sURL.replace("%26", "&");
GeneralRe: %26 --> &memberSimonS5 Jun '04 - 7:32 
Thanks for the info.
 
Cheers,
Simon
 
sig ::
"Don't try to be like Jackie. There is only one Jackie.... Study computers instead.", Jackie Chan on career choices.

article :: animation mechanics in SVG     blog:: brokenkeyboards
"It'll be a cold day in Hell when I do VB.NET...", Chris Maunder

GeneralWindows cannot find the file...memberHawkSoftware25 Mar '02 - 13:51 

I have a simple html page that loads a picture. I really would like to pass parameters to an html page and this article looks like it can solve my problem.
 
Unfortunately when I try the example I have trouble. My "test.htm" file works fine it loads a background image. I added the script function you provided and when I open test.htm it shows an alert box with 0. Then when I try to open "test.htm?mynewname=Thanks" explorer barfs and shows an error message that:
 
"Windows cannot find 'D:/web/html/test.htm?mynewname=Thanks'. Please check the spelling ..."
 
Any guess as to what I am doing wrong?
 
Thanks.
GeneralRe: Windows cannot find the file...membersimons25 Mar '02 - 20:29 
Please can you send me/post the whole page, and I'll figure out the problem.
 
mail : brokenkeyboards@hotmail.com
 
Cheers,
Simon
 
X-5 452 rules.
GeneralRe: Windows cannot find the file...memberAnonymous23 Apr '02 - 20:22 

Simons,
 
Works great. I must have had a cut and paste problem.
 
Thanks for the help!
 
Hawk

GeneralRe: Windows cannot find the file...memberscunn29 May '02 - 6:41 
I had the same problem, but noticed that if I post the page on Tripod, it works just fine. Just a guess, but I think it might have to do with accessing the file on a local drive. I know it doesn't make much sense, but just the same...
 
Hope this helps.
GeneralPlease enhance for passing parameters using bookmark notationmemberVictor Vogelpoel30 Jan '02 - 21:30 
Instead of using the Search notation '?', the bookmark notation '#' can also be used to pass parameters to OTHER pages. So it would be nice to enhance the code for this.
 
Why would you use the bookmark notation for passing parameters? The search notation doesn't work within HTML Help files (.CHM), but you can pass parameters using the bookmark notation! I used this in the electronic photo album to remember the last photo being shown. See http://www.codeproject.com/winhelp/htmlhelpdemoalbum.asp for the article and code (code is not explained, but can be found in the album).
 
I say OTHER pages, because a browser (at least IE) doesn't reload a page when you would call the same page but with different parameters in the # notation. (Chris Maunder actually uses this behaviour in the forums to open a thread without reloading the whole page).
 
Victor
GeneralRe: Please enhance for passing parameters using bookmark notationmembersimons31 Jan '02 - 1:17 
Thanks for the feedback, Victor.
My reasoning for using "?" was because of the web developer's familiarity with the way Request.Querystring works, but I like your idea.
Any idea if there is a delimiter for the bookmark notation. It doesn't really make sense since IE would not know what to do with it. If I use "&" as the delimiter, will this not be against an internet standard?
 
I'll be keen for any more feedback that you have on this.

 
Cheers,
Simon
 
If we fail to anticipate the unforeseen or expect the unexpected in the universe of infinite possibilities, then we may find ourselves at the mercy of anyone or anything that can not be programmed, categorized or easily referenced.
GeneralRe: Please enhance for passing parameters using bookmark notationmemberVictor Vogelpoel31 Jan '02 - 2:52 
I suggest that you use the "&" as delimiter (as I did in the album code). It keeps your code simple as well.
 
Using the bookmark for passing parameters is a workaround for not be able to pass parameters using "?" in an HTML Help file, which I needed in my HTML Help photo album.
In 1999 I prepared some files to demonstrate the problem and the solution. Interested folks can find it at:
http://home01.wxs.nl/~vvogel/htmlhelparg/index.html
 
VictorV
GeneralRe: Please enhance for passing parameters using bookmark notationmembersimons31 Jan '02 - 3:00 

Thanks again for the info, Victor.
You should write a whitepaper on this and send it to RoboHELP, etc...
 
Cheers,
Simon
GeneralRe: Please enhance for passing parameters using bookmark notationmemberVictor Vogelpoel31 Jan '02 - 3:40 
Well, it's in the open anyway now in my photo album article on this site (at least it's mentioned) and I already published it in the WinHelp mailing list a long time ago, so I don't have the urge to start writing.
I'm not too thrilled about eHelp anyway (the creators of RoboHelp).
 
Victor

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

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