Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 8th, 2004, 03:52 PM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing URL in a Querystring

Hello Everyone and thanks for your help in advance. I have a function that is loaded in a web page that pops open a new window to another page. However, I have the new URL hard coded into the page, but would prefer to pass the URL to open in a querystring, thus making it a reusable page. However, I am not sure how to utilize querystrings in Javascript nor how to pass a URL in a querystring. Any help on this would be greatly appreciated. Thanks.

 
Old July 8th, 2004, 04:45 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

The easiest approach would be to use a server language, perhaps PHP, ASP or CGI.

If you have access to any of those, go with that. Otherwise, you'll need a function that parses the querystring and gets the string between the '=' sign and the '&' sign (or the end of the string).

I found this Googling:

function querystring(key)
{
 var value = null;
 for (var i=0;i<querystring.keys.length;i++)
 {
  if (querystring.keys[i]==key)
  {
   value = querystring.values[i];
   break;
  }
 }
 return value;
}

querystring.keys = new Array();
querystring.values = new Array();

function querystring_parse()
{
 var query = window.location.search.substring(1);
 var pairs = query.split("&");

 for (var i=0;i<pairs.length;i++)
 {
  var pos = pairs[i].indexOf('=');
  if (pos >= 0)
  {
   var argname = pairs[i].substring(0,pos);
   var value = pairs[i].substring(pos+1);
   querystring.keys[querystring.keys.length] = argname;
   querystring.values[querystring.values.length] = value;
  }
 }
}
querystring_parse();

Supposedly you can do this:

var x = querystring("x");

....to get the value of any querystring variable.

So if you were at http://www.yoursite.com/folder/something.html?x=hello, using the above code would set x to "hello".

I haven't tried the script, so let me know if it works.

HTH,

Snib

<><
 
Old July 8th, 2004, 04:53 PM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the response. Before I go down this road, I do have access to ASP and ASP.Net, so is there a way to pass this parameter to the Javascript?

 
Old July 8th, 2004, 04:59 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Try this in ASP:

<script type="text/javascript">
var x = "<%=Response.QueryString("x")%>";
</script>

Dunno about ASP.NET, it might be the same for all I know. :-)

HTH,

Snib

<><





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data via querystring LanceRoss Beginning VB 6 3 July 25th, 2008 12:20 PM
passing password in in querystring jaya929 ASP.NET 2.0 Professional 5 May 27th, 2008 07:47 AM
Automatically add querystring paramaters to URL trancehead ASP.NET 2.0 Professional 0 July 24th, 2006 03:03 AM
How do I pass URL to querystring? Shawn Mohan .NET Web Services 0 June 28th, 2006 12:33 AM
Passing a querystring value that contains '&' jfrizelle Classic ASP Databases 3 March 20th, 2006 09:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.