"Aaron Gray" <an********@gma il.comwrote in message
news:4i******** ****@individual .net...
>I put together the following code to get the href's parameters :-
>snip<
What I would like to do is return Undefined if there are no parameters,
how do I do this ?
I think Randy already covered this.
It's nothing more than personal preference but I never liked using
"undefined" for this kind of thing. I personally prefer returning null.
Practically there's very little difference (null and undefined both evaluate
to false) but with null you know that something's been set.
Undefined, on the other hand, could be a meaningful return from a function
or or the result of a typo calling the function. I like to be explicit in
my negatives. ;^) But then again "undefined" does seem very defensible as
the value asked for is, indeed, undefined.
I guess it all comes down to how the argument in your head goes - I wouldn't
steadfastly defend one or the other.
Any other tips that maybe useful ?
I just went through this same exercise a few weeks ago! Here's some things
I found/discovered/hated:
+) Remember that JavaScript has already pulled out the query string for you
into document.locati on.search. You don't really need to do the string
search for a question mark.
+) First off remember that you might get multiple parameters with the same
name: query string parameters do not have to be unique. Depending on your
browser and application you might get this if multiple form fields have the
same name for example. This can be a huge pain but isn't that hard to deal
with.
In my case the method that allows access to the parameters takes a
"ReturnStyl e" argument of "first" (the first value of that name), "last"
(the last value of that name), "List" (a comma-separated list of all the
values) or "Array" (the default - an array of all the values).
That way if you're SURE about the return you can call for the first value
and use it as a simple string... but in general it's no harder to just
access the first element of the returned array.
+) Personally I don't find the order of the paramters to be all that
important. The browser isn't required to send parameters in any specific
order. I ended up (like you did) with an object where each property name
was a named parameter. However, due to the issue above the value of that
property is an array of all the values with that name.
+) 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.
+) I'm not sure if you'll ever have the case... but I sometimes find myself
needing to parse a "foreign" query string - either one that was stored as
part of a URL someplace or constructed from scratch or something. You may
want to consider adding the capability to pass in a query string. In mine I
just added a "QueryStrin g" argument as the last one and did this in the
code:
if ( !QueryString ) {
QueryString = location.search ;
};
All it's saying is "If I got passed one, use it, otherwise use the one from
the current page".
If you'd like to take a look at my code feel free. It's open source and
ready to be poked and prodded:
http://www.depressedpress.com/Conten...ring/Index.cfm
The one feature I'd really like to add is the addition "search engine safe"
type URLs... but the variation amongst them is daunting. They're not "query
strings" but replacements for them which serve the same function when
translated by aware server-software. The point is that search engines will
often index these pages where they might not index a page with a query
string (which is presumed to be dynamic).
For example you might see this (which is generally parsable as "anything
after the first slash after the last period" but I need to do more
research - all of the server-side solutions I've seen look for specific
extensions making me wonder about the prevalence of periods after the file
extension. Also there really doesn't need to be a file extension at all.):
http://www.mysite.com/page.cfm/param.../param2/value2
It's a legal URL and encodes two parameters. However some implementations
use different delimiters like this:
http://www.mysite.com/page.cfm/param.../param2-value2
I've also seen this (which uses server-side mappings and URL rewriting to do
its stuff - no extension):
http://www.mysite.com/page/param1/value1/param2/value2
Or this (where the parameter names are omitted completely... apparently
being infered by the server application):
http://yourdomain.com/yourblog/nfblo...3/sample-post/
In any case it's ending up being a larger task than I first thought. ;^)
Jim Davis