Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 8th, 2004, 03:52 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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.

Reply With Quote
  #2 (permalink)  
Old July 8th, 2004, 04:45 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
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

<><
Reply With Quote
  #3 (permalink)  
Old July 8th, 2004, 04:53 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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?

Reply With Quote
  #4 (permalink)  
Old July 8th, 2004, 04:59 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
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

<><
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:43 AM.


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