Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-18-2005, 05:44 PM   PM User | #1
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
using JS to pull url variables.

url example:

Quote:
whatever.com/index.html?name=username&password=password&gender=male&age=18
Code:
function getE(){
  var url_string = String(document.location);
  var url_array1 = url_string.split('?');
  if(url_array1[1]){
  var e_array = url_array1[1].split('&');
  for(i=0;i<=20;i++){
   if(e_array[i]){
    pullE(e_array[i]);
   }
  }
  }
}   
function pullE(val){
 var useVal = val.split('=');
 alert(useVal[0]+"  ==  "+useVal[1]); //display purposes
}
this would alert in order:
name == username
password == password
gender == male
age == 18

where useVal[0] is the element name and useVal[1] is the value.
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<

Last edited by rlemon; 05-18-2005 at 05:51 PM..
rlemon is offline   Reply With Quote
Old 05-18-2005, 05:46 PM   PM User | #2
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
doesn't have to be two functions. i just like to break it out into two functions so i can compress the top function and still work with the output.
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
rlemon is offline   Reply With Quote
Old 05-18-2005, 05:49 PM   PM User | #3
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
here is it in one function

Code:
function getE(){
       	var url_string = String(document.location);
       	var url_array1 = url_string.split('?');
		if(url_array1[1]){
			var e_array = url_array1[1].split('&');
			for(i=0;i<=(e_array.length);i++){
				if(e_array[i]){
					var useVal = e_array[i].split('=');
					alert(useVal[0]+"  ==  "+useVal[1]);
				}
			} 
		}
}
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
rlemon is offline   Reply With Quote
Old 05-19-2005, 06:31 AM   PM User | #4
Astro-Boy
New Coder

 
Join Date: Jun 2002
Location: Sydney, NSW, Australia
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Astro-Boy is an unknown quantity at this point
Here's an alternate approach;

Code:
function get(name) {
	var q = unescape(location.search.substring(1)).split(/[=&]/);
	for (var j=0; j<q.length; j+=2) {
		if (q[j] == name) {
			return q[j+1];
		}
	}
	return null;
}
Use get("whatever") to retrieve each parameter.
__________________
[ AstroBoy Online | Experiments in Stuff | Google, use it! ]

Last edited by Astro-Boy; 05-20-2005 at 01:17 AM..
Astro-Boy is offline   Reply With Quote
Old 05-19-2005, 01:34 PM   PM User | #5
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
Quote:
Originally Posted by Astro-Boy
Here's an alternate approach;

Code:
function get(name) {
	var q = location.search.substring(1).split(/[=&]/);
	for (var j=0; j<q.length; j+=2) {
		if (q[j] == name) {
			return q[j+1];
		}
	}
	return null;
}
Use get("whatever") to retrieve each parameter.
i'm not see'n it
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
rlemon is offline   Reply With Quote
Old 05-19-2005, 04:13 PM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
There are more robust parse querystring scripts here in CF.

http://www.codingforums.com/showthread.php?t=25786
http://www.codingforums.com/showthre...&threadid=4555

Your script did not consider important things such as unescaping the name and value pair and replacing +'s with spaces.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 05-19-2005, 05:30 PM   PM User | #7
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
Quote:
Originally Posted by glenngv
There are more robust parse querystring scripts here in CF.

http://www.codingforums.com/showthread.php?t=25786
http://www.codingforums.com/showthre...&threadid=4555

Your script did not consider important things such as unescaping the name and value pair and replacing +'s with spaces.
I agree it's not the best method of pulling url vars.
however i specifically wrote it for an application i'm writing, figured other people might find it usefull so... posted it here
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
rlemon is offline   Reply With Quote
Old 05-20-2005, 01:22 AM   PM User | #8
Astro-Boy
New Coder

 
Join Date: Jun 2002
Location: Sydney, NSW, Australia
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Astro-Boy is an unknown quantity at this point
Quote:
Originally Posted by rlemon
i'm not see'n it
I'm an idiot and forgot to unescape()

Code:
function get(name) {
	var q = unescape(location.search.substring(1)).split(/[=&]/);
	for (var j=0; j<q.length; j+=2) {
		if (q[j] == name) {
			return q[j+1];
		}
	}
	return null;
}
This is what happens when you don't test your code people!
__________________
[ AstroBoy Online | Experiments in Stuff | Google, use it! ]
Astro-Boy is offline   Reply With Quote
Old 05-20-2005, 02:08 AM   PM User | #9
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Quote:
Originally Posted by john33
Since a server-side language (ASP) is used here, you should use it to parse the querystring and not use javascript.
Code:
<%
dim user
user = Request.QueryString("login")
...
%>
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 05-20-2005, 02:40 PM   PM User | #10
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
no ServerSideLang is being used glenn

edit: ohh that guys post. gotcha.
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
rlemon is offline   Reply With Quote
Old 06-08-2005, 10:12 PM   PM User | #11
MaGnA
New Coder

 
Join Date: Jun 2005
Location: Toronto
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
MaGnA is an unknown quantity at this point
Code:
function get(name) {
	var q = unescape(location.search.substring(1)).split(/[=&]/);
	for (var j=0; j<q.length; j+=2) {
		if (q[j] == name) {
			return q[j+1];
		}
	}
	return null;
}
Of course, by "j += 2" you're assuming that the query args would always come in name/value pairs and you wouldn't have single name tokens such as:

?name1=value1&orphantoken&name2=value&...

It might be a better idea to first split by "&", and then by "=", in that order.

Also, since the function is splitting and scanning the entire query string each time a name is queried, it's a bit inefficient (but, hey, this is an interpreted language anyways -- who cares about that much efficiency )

While we're at parsing the entire query string and strolling through the tokens in a for loop, we could populate a global associative array so that we can simply access the value we want through something like g_querystring["name1"].

Code:
function parseQueryString()
{
    var obj = new Object();

    var nvpairs = location.search.substring(1).split("&");

    for (var idx = 0; idx < nvpairs.length; idx++)
    {
        var tokens = nvpairs[idx].split("=");

        obj[unescape(tokens[0])] = tokens.length == 2 ?
            unescape(tokens[1]) : undefined;
    }
    
    return obj;
} 

var g_querystring = parseQueryString();

document.write("a=" + g_querystring["a"] + "<br>");
document.write("b=" + g_querystring["b"] + "<br>");
document.write("c=" + g_querystring["c"] + "<br>");

// Iterate through all name/value pairs
for (var name in g_querystring)
{
    document.write(name + "=" + g_querystring[name] + "<br>");
}
One could even go about calling this global object "Request.QueryString" as an ode to ASP

Code:
var Request = new Object();
Request.QueryString = parseQueryString();
alert(Request.QueryString["foo"]);
__________________
magnetiq.com | Twofifty | browsersize.com

Last edited by MaGnA; 06-08-2005 at 11:26 PM..
MaGnA is offline   Reply With Quote
Old 06-13-2005, 04:31 PM   PM User | #12
MaGnA
New Coder

 
Join Date: Jun 2005
Location: Toronto
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
MaGnA is an unknown quantity at this point
This version handles decoding the "+" as well and hence is the correct way of doing the URL-decoding:

Code:
function urlDecode(s)
{
    return unescape(s.replace("+", " "));
}

function parseQueryString()
{
    var obj = new Object();

    var nvpairs = location.search.substring(1).split("&");

    for (var idx = 0; idx < nvpairs.length; idx++)
    {
        var tokens = nvpairs[idx].split("=");

        obj[urlDecode(tokens[0])] = tokens.length == 2 ?
            urlDecode(tokens[1]) : undefined;
    }
    
    return obj;
}
__________________
magnetiq.com | Twofifty | browsersize.com
MaGnA is offline   Reply With Quote
Old 06-14-2005, 06:24 AM   PM User | #13
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
I stand by my suggestion of using ASP to parse the querystrings since an .asp page is used.

But if in case of not using asp (or any server-side language for that matter) and the only choice is javascript, the links I posted in my post #7 are more robust parse querystring scripts as they not only cover decoding of + but also allow multiple values which I think no scripts posted here do.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 06-14-2005, 02:31 PM   PM User | #14
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
Quote:
Originally Posted by glenngv
I stand by my suggestion of using ASP to parse the querystrings since an .asp page is used.

But if in case of not using asp (or any server-side language for that matter) and the only choice is javascript, the links I posted in my post #7 are more robust parse querystring scripts as they not only cover decoding of + but also allow multiple values which I think no scripts posted here do.
hehe
MINE DOES!! it doesn't decode that stuff tho. i call it a trade off (actually i built it for a specific purpose and never have to decode the values i'm passing but i'm passing multiple values so i know mine works.
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
rlemon is offline   Reply With Quote
Old 07-05-2005, 01:34 PM   PM User | #15
toddaa
New to the CF scene

 
Join Date: Jul 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
toddaa is an unknown quantity at this point
Can you guys suggest anything for doing this on a local HTML page. To be more specific...I want to do this on a CD. I've tried rlemon single function example, but for some reason it didnt work. In my own troubleshooting I have tried to alert the varialbe containing the enture URL. Anything after the ? doesnt even show up...nor does the ?

Is this a javascript thing or a browser thing? Can anyonhe help?

Thanks

*******

Nevermind...I believe I solved my own problem. I didn't have file:/// in front of my local path to the page. I was testing with IE and when trying FireFox...it new it was local and put it in for me. When doing it in IE the script worked fine.

Thanks again.

Last edited by toddaa; 07-05-2005 at 01:40 PM..
toddaa is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 12:11 PM.


Advertisement
Log in to turn off these ads.