Click here to Skip to main content
Email Password   helpLost your password?

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralExcelent Simon
Anonymous
3:39 1 Apr '05  
Well done.
General%26 --> &
JMS_
7:04 5 Jun '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 --> &
SimonS
8:32 5 Jun '04  
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...
HawkSoftware
14:51 25 Mar '02  

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...
simons
21:29 25 Mar '02  
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...
Anonymous
21:22 23 Apr '02  

Simons,

Works great. I must have had a cut and paste problem.

Thanks for the help!

Hawk

GeneralRe: Windows cannot find the file...
scunn
7:41 29 May '02  
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 notation
Victor Vogelpoel
22:30 30 Jan '02  
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 notation
simons
2:17 31 Jan '02  
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 notation
Victor Vogelpoel
3:52 31 Jan '02  
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 notation
simons
4:00 31 Jan '02  

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 notation
Victor Vogelpoel
4:40 31 Jan '02  
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


Last Updated 30 Jan 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010