By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,730 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,730 IT Pros & Developers. It's quick & easy.

getting variable from query string

P: n/a
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>{)}

Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

juglesh wrote:
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>{)}


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

Jul 23 '05 #2

P: n/a
On Tue, 21 Dec 2004 16:09:41 GMT, juglesh <ju*****@nospamRadioKDUG.com>
wrote:

[snip]
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"


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.
Jul 23 '05 #3

P: n/a

"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

juglesh wrote:
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>{)}


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


thanks, this is working fine.


Jul 23 '05 #4

P: n/a

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsjdatha5x13kvk@atlantis...
On Tue, 21 Dec 2004 16:09:41 GMT, juglesh <ju*****@nospamRadioKDUG.com>
wrote:

[snip]
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"


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' */
}


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

Jul 23 '05 #5

P: n/a
On Tue, 21 Dec 2004 18:47:22 GMT, juglesh <ju*****@nospamRadioKDUG.com>
wrote:

[snip]
[...] i dont understand yours.
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).
where is MyVar?


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.
Jul 23 '05 #6

P: n/a
In article <opsjdit6cgx13kvk@atlantis>,
"Michael Winter" <M.******@blueyonder.co.invalid> wrote:
On Tue, 21 Dec 2004 18:47:22 GMT, juglesh <ju*****@nospamRadioKDUG.com>
wrote:

[snip]
[...] i dont understand yours.


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).


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] : '';
}
Jul 23 '05 #7

P: n/a
On Wed, 22 Dec 2004 13:34:19 -0600, Chris Riesbeck <cr*******@yahoo.com>
wrote:

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


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.
Jul 23 '05 #8

P: n/a
In article <opsjfdvgmvx13kvk@atlantis>,
"Michael Winter" <M.******@blueyonder.co.invalid> wrote:
On Wed, 22 Dec 2004 13:34:19 -0600, Chris Riesbeck <cr*******@yahoo.com>
wrote:

[snip]
function getQval(name)
{
var re = new RegExp('[?&]' + escape(name) + '=([^&$]+)';
Shouldn't the dollar ($) be a hash (#)?


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?
Also the escape call shouldn't necessary; you know what you're
looking for in the URL.


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?
Jul 23 '05 #9

P: n/a
On Thu, 23 Dec 2004 15:53:07 -0600, Chris Riesbeck <cr*******@yahoo.com>
wrote:

[snip]
Why a # though? Is one legal at the point in a URL?


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

[MW:]
Also the escape call shouldn't necessary; you know what you're looking
for in the URL.


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?


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.
Jul 23 '05 #10

P: n/a
On Thu, 23 Dec 2004 22:57:45 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:
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 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.
Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.