Connecting Tech Pros Worldwide Forums | Help | Site Map

getting variable from query string

juglesh
Guest
 
Posts: n/a
#1: Jul 23 '05
hello, I've put in my time searching, so I'll ask now<G>

I need to look at the url, if a variable is present in the query string.

so, if my address bar looks like:
http://www.domain.com?THIS=yes&that=...otherthing=154

I need it to look for THIS
if THIS is present, and THIS= yes
then MyVar = "my string here"
if THIS does not = yes
then MyVar = "not yes string here"
if THIS is not present
then MyVar = "not yes string here"


--

thanks for your time,
juglesh B>{)}




RobB
Guest
 
Posts: n/a
#2: Jul 23 '05

re: getting variable from query string



juglesh wrote:[color=blue]
> hello, I've put in my time searching, so I'll ask now<G>
>
> I need to look at the url, if a variable is present in the query[/color]
string.[color=blue]
>
> so, if my address bar looks like:
> http://www.domain.com?THIS=yes&that=...otherthing=154
>
> I need it to look for THIS
> if THIS is present, and THIS= yes
> then MyVar = "my string here"
> if THIS does not = yes
> then MyVar = "not yes string here"
> if THIS is not present
> then MyVar = "not yes string here"
>
>
> --
>
> thanks for your time,
> juglesh B>{)}[/color]

function getQval(name)
{
var m, Q = window.location.search.substring(1);
if ('' != Q)
{
var re = new RegExp(escape(name) + '=([^&$]+)');
if (m = Q.match(re))
return m[1];
else return '';
}
return '';
}

var MyVar = (getQval('THIS') == 'yes') ?
'my string here' : 'not yes string here';

Michael Winter
Guest
 
Posts: n/a
#3: Jul 23 '05

re: getting variable from query string


On Tue, 21 Dec 2004 16:09:41 GMT, juglesh <juglesh@nospamRadioKDUG.com>
wrote:

[snip]
[color=blue]
> http://www.domain.com?THIS=yes&that=...otherthing=154
>
> I need it to look for THIS
> if THIS is present, and THIS= yes
> then MyVar = "my string here"
> if THIS does not = yes
> then MyVar = "not yes string here"
> if THIS is not present
> then MyVar = "not yes string here"[/color]

var url = document.URL,
i = url.indexOf('THIS='),
j = url.indexOf('&', i);
if(-1 == j) {j = url.length;}
if((-1 == i) || ('yes' != url.substring(i + 5, j))) {
/* 'THIS' doesn't exist or is not equal to 'yes' */
}

or for something more generic:

/* Returns the value associated with a key in the query string.
*
* If the key doesn't exist or doesn't have a value, undefined
* is returned.
*/
function getQueryValue(name) {
var url = document.URL,
i = url.indexOf(name += '='),
j = url.indexOf('&', i);
if(-1 == j) {j = url.length;}
if(-1 != i) {return url.substring(i + name.length, j);}
}

if('yes' != getQueryValue('THIS')) {
/* 'THIS' doesn't exist or is not equal to 'yes' */
}

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
juglesh
Guest
 
Posts: n/a
#4: Jul 23 '05

re: getting variable from query string



"RobB" <ferndoc9@hotmail.com> wrote in message
news:1103648254.379190.325870@c13g2000cwb.googlegr oups.com...[color=blue]
>
> juglesh wrote:[color=green]
>> hello, I've put in my time searching, so I'll ask now<G>
>>
>> I need to look at the url, if a variable is present in the query[/color]
> string.[color=green]
>>
>> so, if my address bar looks like:
>> http://www.domain.com?THIS=yes&that=...otherthing=154
>>
>> I need it to look for THIS
>> if THIS is present, and THIS= yes
>> then MyVar = "my string here"
>> if THIS does not = yes
>> then MyVar = "not yes string here"
>> if THIS is not present
>> then MyVar = "not yes string here"
>>
>>
>> --
>>
>> thanks for your time,
>> juglesh B>{)}[/color]
>
> function getQval(name)
> {
> var m, Q = window.location.search.substring(1);
> if ('' != Q)
> {
> var re = new RegExp(escape(name) + '=([^&$]+)');
> if (m = Q.match(re))
> return m[1];
> else return '';
> }
> return '';
> }
>
> var MyVar = (getQval('THIS') == 'yes') ?
> 'my string here' : 'not yes string here';[/color]

thanks, this is working fine.




juglesh
Guest
 
Posts: n/a
#5: Jul 23 '05

re: getting variable from query string



"Michael Winter" <M.Winter@blueyonder.co.invalid> wrote in message
news:opsjdatha5x13kvk@atlantis...[color=blue]
> On Tue, 21 Dec 2004 16:09:41 GMT, juglesh <juglesh@nospamRadioKDUG.com>
> wrote:
>
> [snip]
>[color=green]
>> http://www.domain.com?THIS=yes&that=...otherthing=154
>>
>> I need it to look for THIS
>> if THIS is present, and THIS= yes
>> then MyVar = "my string here"
>> if THIS does not = yes
>> then MyVar = "not yes string here"
>> if THIS is not present
>> then MyVar = "not yes string here"[/color]
>
> var url = document.URL,
> i = url.indexOf('THIS='),
> j = url.indexOf('&', i);
> if(-1 == j) {j = url.length;}
> if((-1 == i) || ('yes' != url.substring(i + 5, j))) {
> /* 'THIS' doesn't exist or is not equal to 'yes' */
> }
>
> or for something more generic:
>
> /* Returns the value associated with a key in the query string.
> *
> * If the key doesn't exist or doesn't have a value, undefined
> * is returned.
> */
> function getQueryValue(name) {
> var url = document.URL,
> i = url.indexOf(name += '='),
> j = url.indexOf('&', i);
> if(-1 == j) {j = url.length;}
> if(-1 != i) {return url.substring(i + name.length, j);}
> }
>
> if('yes' != getQueryValue('THIS')) {
> /* 'THIS' doesn't exist or is not equal to 'yes' */
> }
>[/color]

ok, thanks for the reply, i'm using RobB's solution, one reason is i dont
understand yours. where is MyVar?



Michael Winter
Guest
 
Posts: n/a
#6: Jul 23 '05

re: getting variable from query string


On Tue, 21 Dec 2004 18:47:22 GMT, juglesh <juglesh@nospamRadioKDUG.com>
wrote:

[snip]
[color=blue]
> [...] i dont understand yours.[/color]

What's to understand?

/* Get the address of the document. */
var url = document.URL,
/* Find the location of the string, 'THIS=', within the URL.
*
* If the sub-string doesn't exist, i will be -1.
*/
i = url.indexOf('THIS='),
/* Find the first ampersand (&) after the sub-string. This
* will indicate where the value ends.
*/
j = url.indexOf('&', i);
/* If no ampersand was found, assume the name/value pair is the
* last in the URL. That is, the end of the value is the end of
* the string.
*/
if(-1 == j) {j = url.length;}
if((-1 == i) || ('yes' != url.substring(i + 5, j))) {
/* 'THIS' doesn't exist or is not equal to 'yes' */
}

I think that's simpler than Rob's (no offense Rob :P).
[color=blue]
> where is MyVar?[/color]

I assumed that you would be capable of adding a variable declaration and
assignment yourself. Was I mistaken?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Chris Riesbeck
Guest
 
Posts: n/a
#7: Jul 23 '05

re: getting variable from query string


In article <opsjdit6cgx13kvk@atlantis>,
"Michael Winter" <M.Winter@blueyonder.co.invalid> wrote:
[color=blue]
> On Tue, 21 Dec 2004 18:47:22 GMT, juglesh <juglesh@nospamRadioKDUG.com>
> wrote:
>
> [snip]
>[color=green]
> > [...] i dont understand yours.[/color]
>
> What's to understand?
>
> /* Get the address of the document. */
> var url = document.URL,
> /* Find the location of the string, 'THIS=', within the URL.
> *
> * If the sub-string doesn't exist, i will be -1.
> */
> i = url.indexOf('THIS='),
> /* Find the first ampersand (&) after the sub-string. This
> * will indicate where the value ends.
> */
> j = url.indexOf('&', i);
> /* If no ampersand was found, assume the name/value pair is the
> * last in the URL. That is, the end of the value is the end of
> * the string.
> */
> if(-1 == j) {j = url.length;}
> if((-1 == i) || ('yes' != url.substring(i + 5, j))) {
> /* 'THIS' doesn't exist or is not equal to 'yes' */
> }
>
> I think that's simpler than Rob's (no offense Rob :P).[/color]

I'm with Rob. I'd rather maintain a one-line match over
two indexOf() calls, a check against length, and a substring.

I would however change his code slightly, to avoid getting
the wrong parameter for a URL like

foo.html?NOTTHIS=yes&THIS=no


function getQval(name)
{
var re = new RegExp('[?&]' + escape(name) + '=([^&$]+)'; // added [?&]
var m = re.exec(window.location.search); // no substr
return m ? m[1] : '';
}
Michael Winter
Guest
 
Posts: n/a
#8: Jul 23 '05

re: getting variable from query string


On Wed, 22 Dec 2004 13:34:19 -0600, Chris Riesbeck <criesbeck@yahoo.com>
wrote:

[snip]
[color=blue]
> function getQval(name)
> {
> var re = new RegExp('[?&]' + escape(name) + '=([^&$]+)';[/color]

Shouldn't the dollar ($) be a hash (#)? A dollar has no significance in a
HTTP URL. Also the escape call shouldn't necessary; you know what you're
looking for in the URL.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Chris Riesbeck
Guest
 
Posts: n/a
#9: Jul 23 '05

re: getting variable from query string


In article <opsjfdvgmvx13kvk@atlantis>,
"Michael Winter" <M.Winter@blueyonder.co.invalid> wrote:
[color=blue]
> On Wed, 22 Dec 2004 13:34:19 -0600, Chris Riesbeck <criesbeck@yahoo.com>
> wrote:
>
> [snip]
>[color=green]
> > function getQval(name)
> > {
> > var re = new RegExp('[?&]' + escape(name) + '=([^&$]+)';[/color]
>
> Shouldn't the dollar ($) be a hash (#)?[/color]

Actually, I meant to delete the $ - it was in Rob's code but it's
not relevant here.

Why a # though? Is one legal at the point in a URL?
[color=blue]
> Also the escape call shouldn't necessary; you know what you're
> looking for in the URL.[/color]

Again, that was from Rob's code -- but isn't that correct? If
the parameter was "my param" I'd want to be looking for
my%20param, no?
Michael Winter
Guest
 
Posts: n/a
#10: Jul 23 '05

re: getting variable from query string


On Thu, 23 Dec 2004 15:53:07 -0600, Chris Riesbeck <criesbeck@yahoo.com>
wrote:

[snip]
[color=blue]
> Why a # though? Is one legal at the point in a URL?[/color]

The fragment identifier (used to target anchors). If one's present, the
last value will be followed immediately by a hash.

[MW:][color=blue][color=green]
>> Also the escape call shouldn't necessary; you know what you're looking
>> for in the URL.[/color]
>
> Again, that was from Rob's code -- but isn't that correct? If the
> parameter was "my param" I'd want to be looking for my%20param, no?[/color]

Yes, but you know the space will escaped as %20. Moreover, the escape
function works in the context of an entire URL, which means that some
characters would be allowed unescaped, such as a forward slash (/). The
newer encodeURIComponent function is better, but it's only supported on
newer browsers. Of course, it's trivial to write a substitute:

if('function' != typeof encodeURIComponent) {
encodeURIComponent = function(uri) {
var x = /[\w.!~*'()-]/, r = '';
for(var c, i = 0, n = uri.length; i < n; ++i) {
if(x.test(c = uri.charAt(i))) {r += c;}
else {r += '%' + uri.charCodeAt(i).toString(16).toUpperCase();}
}
return r;
};
}

That doesn't completely follow the specification, but that's only a
problem if you're escaping Unicode strings.

I'm assuming that Number.prototype.toString is well supported - it was
defined in early versions of JavaScript and JScript so other browsers
should have implemented it too.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Michael Winter
Guest
 
Posts: n/a
#11: Jul 23 '05

re: getting variable from query string


On Thu, 23 Dec 2004 22:57:45 GMT, Michael Winter
<M.Winter@blueyonder.co.invalid> wrote:
[color=blue]
> if('function' != typeof encodeURIComponent) {
> encodeURIComponent = function(uri) {
> var x = /[\w.!~*'()-]/, r = '';
> for(var c, i = 0, n = uri.length; i < n; ++i) {
> if(x.test(c = uri.charAt(i))) {r += c;}
> else {
> r += '%' + uri.charCodeAt(i).toString(16).toUpperCase();
> }
> }
> return r;
> };
> }[/color]

That would give incorrect results for characters less than 0x10.

if('function' != typeof encodeURIComponent) {
this.encodeURIComponent = function(uri) {
var x = /[\w.!~*'()-]/, r = '';
for(var c, i = 0, n = uri.length; i < n; ++i) {
if(x.test(c = uri.charAt(i))) {r += c;}
else {
c = uri.charCodeAt(i).toString(16).toUpperCase();
r += ((c.length == 1) ? '%0' : '%') + c;
}
}
return r;
};
}

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Closed Thread