p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   Passing URL in a Querystring (http://p2p.wrox.com/showthread.php?t=15181)

hugh@kmcnetwork.com July 8th, 2004 03:52 PM

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.

Snib July 8th, 2004 04:45 PM

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];
 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;

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.




hugh@kmcnetwork.com July 8th, 2004 04:53 PM

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?

Snib July 8th, 2004 04:59 PM

Try this in ASP:

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

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




All times are GMT -4. The time now is 08:31 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.