...

View Full Version : using JS to pull url variables.



rlemon
05-18-2005, 05:44 PM
url example:


whatever.com/index.html?name=username&password=password&gender=male&age=18



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.

rlemon
05-18-2005, 05:46 PM
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.

rlemon
05-18-2005, 05:49 PM
here is it in one function



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

Astro-Boy
05-19-2005, 06:31 AM
Here's an alternate approach;


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.

rlemon
05-19-2005, 01:34 PM
Here's an alternate approach;


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

glenngv
05-19-2005, 04:13 PM
There are more robust parse querystring scripts here in CF.

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

Your script did not consider important things such as unescaping the name and value pair and replacing +'s with spaces.

rlemon
05-19-2005, 05:30 PM
There are more robust parse querystring scripts here in CF.

http://www.codingforums.com/showthread.php?t=25786
http://www.codingforums.com/showthread.php?s=&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 :cool:

Astro-Boy
05-20-2005, 01:22 AM
i'm not see'n it

I'm an idiot and forgot to unescape() :p


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! ;)

glenngv
05-20-2005, 02:08 AM
tell me:
www.dologin.com/index.asp?login=user&password=pword&name=mike
Since a server-side language (ASP) is used here, you should use it to parse the querystring and not use javascript.

<%
dim user
user = Request.QueryString("login")
...
%>

rlemon
05-20-2005, 02:40 PM
no ServerSideLang is being used glenn :confused:

edit: ohh that guys post. gotcha.

MaGnA
06-08-2005, 10:12 PM
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"].


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 :p


var Request = new Object();
Request.QueryString = parseQueryString();
alert(Request.QueryString["foo"]);

MaGnA
06-13-2005, 04:31 PM
This version handles decoding the "+" as well and hence is the correct way of doing the URL-decoding:


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

glenngv
06-14-2005, 06:24 AM
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.

rlemon
06-14-2005, 02:31 PM
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.

toddaa
07-05-2005, 01:34 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum