Getting URL parameters

A

Aaron Gray

I put together the following code to get the href's parameters :-

function GetParameters()
{
var arg = new Object();
var href = document.location.href;

if ( href.indexOf( "?") != -1)
{
var params = href.split( "?")[1];
var param = params.split("&");

for (var i = 0; i < param.length; ++i)
{
var name = param.split( "=")[0];
var value = param.split( "=")[1];

arg[name] = value;
}
}
return arg;
}

document.open();

var args = GetParameters();
document.write( "date = " + args["date"] + "<BR>");
document.write( "time = " + args["time"] + "<BR>");

document.close();

What I would like to do is return Undefined if there are no parameters, how
do I do this ?

Any other tips that maybe useful ?

Aaron
 
R

Randy Webb

Aaron Gray said the following on 7/23/2006 1:23 PM:
I put together the following code to get the href's parameters :-

function GetParameters()
{
var arg = new Object();
var href = document.location.href;

if ( href.indexOf( "?") != -1)
{

var arg = new Object();


Now, arg will only get defined if there is a ? in the URL.
But, arg should be an Array, not an Object.

var params = href.split( "?")[1];
var param = params.split("&");

for (var i = 0; i < param.length; ++i)
{
var name = param.split( "=")[0];
var value = param.split( "=")[1];

arg[name] = value;
}
}
return arg;



Now, if arg gets defined, it will return arg. If it is not defined, then
undefined will get returned.
}

document.open();

var args = GetParameters();
document.write( "date = " + args["date"] + "<BR>");
document.write( "time = " + args["time"] + "<BR>");

document.close();

What I would like to do is return Undefined if there are no parameters, how
do I do this ?

See above.
 
A

Aaron Gray

if ( href.indexOf( "?") != -1)
var arg = new Object();


Now, arg will only get defined if there is a ? in the URL.
okay.

But, arg should be an Array, not an Object.

okay, thats what I had before but it did not report a length when it had
elements so I presumed it was an Object.

So that is an associative Array not an Object ?

Aaron
 
A

Aaron Gray

Now, arg will only get defined if there is a ? in the URL.

Is there a better way of returning a non result than using undefined ?

The associative use of an Array appears to have no length, otherwise I would
use length as an indicator :(

Aaron
 
R

Randy Webb

Aaron Gray said the following on 7/23/2006 4:01 PM:
Is there a better way of returning a non result than using undefined ?

The associative use of an Array appears to have no length, otherwise I would
use length as an indicator :(

Aside from the fact that Javascript doesn't have an Associative Array,
you can check for it's existance:

if (arg){
return arg
}else{
return somethingElse
}
 
L

Lasse Reichstein Nielsen

Jim Davis said:
+) One note: remember that query strings may be URL encoded (have special
characters escaped). Use the JavaScript "unescape()" method to unencode
them for use - you should unescape both the parameter names and the values.

Also remember that spaces in the escaped string have been converted to
"+"'es, so to correctly decode the name or value, you can do:

unescape(string.replace(/\+/g," "))

....
if ( !QueryString ) {
QueryString = location.search;
};

Personal preference (and nothing else) makes me prefer to write it:
QueryString = QueryString || location.search;

The effect is the same, it's slightly shorter and slightly less
efficient if QueryString is not falsey.

/L
 
A

Aaron Gray

Lasse Reichstein Nielsen said:
Also remember that spaces in the escaped string have been converted to
"+"'es, so to correctly decode the name or value, you can do:

unescape(string.replace(/\+/g," "))

...


Personal preference (and nothing else) makes me prefer to write it:
QueryString = QueryString || location.search;

The effect is the same, it's slightly shorter and slightly less
efficient if QueryString is not falsey.

I am generating the query strings myself from a form on the page so I have
none of these problems.

Interesting though.

Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top