ldiuf 06-19-2002, 06:04 PM Hi,
With ASP when I want get a value via links, or what have you, I do this...
request("this") or request.querystring("this")
My question is, is there a way to do this with javascript and if so how?
Thanks,
Larry
jalarie 06-19-2002, 06:27 PM The following picks up the ENTIRE query string including the leading question (?) mark:
q=document.location.search.substring(0);
To skip the question mark:
q=document.location.search.substring(1);
Either way, then you have to parse the string:
a=new Array();
a=q.split('&');
Each item in the array will contain a lable, an equals (=) sign, and a value. Split them up with:
b=new Array();
b=a[index].split('=');
JohnKrutsch 06-19-2002, 08:21 PM I like using this setup:
<a href="=querrystring2.htm?size=XXXL&type=Cotton%20T%20Shirt">test</a>
<script type="text/javascript">
<!--//
if (location.search){
var vals=location.search.substr(1).split("&");
var request= new Array();
for (var i in vals) {
vals[i] = vals[i].replace(/\+/g, " ").split("=");
request[unescape(vals[i][0])] =unescape(vals[i][1]);
}
}
//-->
</script>
Then you can just use javascript to refer to items like so:
request["size"]
which with this example would return XXXL
ldiuf 06-19-2002, 08:25 PM JohnKrutsch,
That is awesome :o that's exactly what I was looking for.
Thanks a Bunch,
Larry:thumbsup:
whammy 06-20-2002, 01:06 AM That does indeed look handy... kind of a way to fool javascript into acting like ASP. If I need to use stuff from the querystring without a server-side language, I'm saving this!
P.S. That means Jalarie's too... that also looks like a very straightforward way of doing it!
;)
Dave Clark 06-20-2002, 01:18 AM I use these that I wrote:
// Copyright (c) 2001 by Dave Clark Consulting, Springdale, AR -- www.DaveClarkConsulting.com -- all rights reserved.
// ---------------------------------------- //
// Constructor function creates hash array from Search String.
// Use as follows, e.g.:
// var QueryString = new SearchStringExtract(self.location);
// var QueryString = new SearchStringExtract(window.location);
// var QueryString = new SearchStringExtract(self.top.location);
// var QueryString = new SearchStringExtract(window.top.location);
//
function SearchStringExtract(locObj) {
var i, inpt = locObj.search.substr(1);
if (inpt.length > 0) {
var ary = inpt.replace(/\+/g, " ").split("&");
for (i in ary) {
ary[i] = ary[i].split("=");
this[unescape(ary[i][0])] = unescape(ary[i][1]);
}
}
}
// ---------------------------------------- //
// Normal function to process Search String into global variables.
// Use as follows, e.g.:
// processSearchString(self.location);
// processSearchString(window.location);
// processSearchString(self.top.location);
// processSearchString(window.top.location);
//
function processSearchString(locObj) {
var i, inpt = locObj.search.substr(1);
if (inpt.length > 0) {
var ary = inpt.replace(/\+/g, " ").split("&");
for (i in ary) {
ary[i] = ary[i].split("=");
window[ary[i][0]] = unescape(ary[i][1]);
}
}
}
// ---------------------------------------- //
Free to use -- not to sell. :)
whammy 06-20-2002, 01:24 AM Sweet... the javascript fairy has shown up! :)
(P.S. I didn't stick you with that moniker, Dave... heheh)
I'm going to be messing with this stuff for awhile... but what a wealth of querystring information.
Thank $MS that they made it easy on the server-side.
P.S. How easy is this in PHP? (I'm sure someone will pipe up here!).
:D
Dave Clark 06-20-2002, 04:02 AM Originally posted by whammy
Sweet... the javascript fairy has shown up! :)
(P.S. I didn't stick you with that moniker, Dave... heheh)
:eek: And just who, might I ask, did?!? :p
And, who's :rolleyes: been talkin' 'bout me behind my back? :(
And, what did I do :o to deserve that moniker?!? :mad:
No Fear! :) I've got it made in the shade! ;) I'm cool! :cool:
mordred 06-20-2002, 10:32 AM Originally posted by whammy
P.S. How easy is this in PHP? (I'm sure someone will pipe up here!).
You asked for it:
1.
in_array("needle", $HTTP_GET_VARS);
2.
extract($HTTP_GET_VARS);
Newer PHP versions do use $_GET, my example should be backwards compatible with older PHP versions.
And yes, I know I'm slightly offtopic here. CNR.
JohnKrutsch 06-20-2002, 03:38 PM Actually in PHP you do not have to do anything special at all to get info out of the query string. If you are at this url:
http://www.someWebStore.com/products.htm?size=XXXL&type=Cotton%20T%20Shirt
all you would need to do to access the information is to simply ask for the item like so:
$size
so to print the size that was passed in the querystring in php you would only have to go like this:
echo $size;
Php does all of the hard work for you, you dont even have to "request" it like in ASP. It figures that it must be a variable you want to use since you passed it along in the first place.
BUT I DIGRESS...after all this is hte JavaScript forum ;)
mordred 06-20-2002, 09:17 PM Going further offtopic, but I have to respond:
JohnKrutsch, what you said about PHP was true before the recent version 4.2 disabled register_globals in the default .ini file. This was done due to security reasons. Of course you may still change this .ini setting, but I don't know if it's the best idea to override the advice of the PHP developing team.
Dave Clark 06-20-2002, 11:51 PM Originally posted by JohnKrutsch
Php does all of the hard work for you, you dont even have to "request" it like in ASP. It figures that it must be a variable you want to use since you passed it along in the first place.
Technically, you aren't "requesting" anything from within ASP either. ASP also does all the "hard" work for you. The only difference is that PHP chose to extract the query string into unstructured individual variables that you reference individually and ASP chose to extract the query string into an object structure from which you reference the data collectively.
<soapbox>
It's too bad that people have to take occasional pot-shots at other languages, presumably, just because they don't happen to like that language. Every language has its value both in application and to the people using it. It is not necessary (or polite) to have to "tear down" one thing in order to try and "build up" another thing.
</soapbox>
OK? :)
JohnKrutsch 06-21-2002, 02:56 PM Hmm.. I don't see any pot shots being taken here. I simply see people disscussing the different ways to get info out of the querystring.
I am one of the few people I know who can actually effectively develop web applications on an IIS box using ASP and SQL Server or MS Access, as well as, PHP/mySQL on an Apache server. I let my clients dictate that.
If my comments were viewed as "tearing down" ASP in order to "build up" PHP that was not my intention. I was simply making a comparison.
I do however agree whole heartedly with you that when this is the case it only stirs the hornets nest.
My appoligies if my intentions were unclear. You should all know that Dave Clark is one of the best scrippters I have had the good fortune to associate myself with. His contirbutions have made me a better scriptter. IMHO when Dave Gets on a soap box it would be wise to listen.
Dave Clark 06-21-2002, 11:00 PM Originally posted by JohnKrutsch
My appoligies if my intentions were unclear.
OK, peace. :) But, John, I feel like I've been put on a pedestal that I don't deserve. :( Besides, it's embarrassing. :o
nolachrymose 06-22-2002, 05:31 PM I've made a little script like John's for those too high on ASP to notice the discrepancy(sp?) between ASP and JS :rolleyes::
function QSHandler() {
var qs=location.search.substr(1).split("&");
this.data=[];
for(var i=0;i<qs.length;i++) this.data[qs[i].split("=")[0]]=qs[i].split("=")[1];
this.QueryString=function(x) {
return this.data[x];
};
} var Request=new QSHandler();
You can get querystring values like so:
var name=Request.QueryString("name");
Hope that helps!
Happy coding! :)
Dave Clark 06-22-2002, 07:30 PM Originally posted by nolachrymose
I've made a little script like John's for those too high on ASP to notice the discrepancy(sp?) between ASP and JS :rolleyes::
Yeah, but... :confused: What's your point? :rolleyes: ...or, did you have one? :)
joh6nn 06-22-2002, 07:37 PM Dave, i swear i had nothing to do with this: javascript fairy (http://freewarejava.com/ubb/Forum1/HTML/026402.html)
JohnKrutsch 06-22-2002, 07:44 PM Ok, well I think thishas served its purpose.
I am cloisng it now but with all the good querystring info I am sure it will continue to get lots of views.
|
|