Forum Moderators: open

Message Too Old, No Replies

Checking for GET variables on end of URL

         

londrum

1:40 pm on Apr 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hello. i've got a little function which is only supposed to run on pages with a specific GET variable. So i've got the following line in it, which works okay

if(location.href=='http://www.example.com/dir/page.php?var=1')


the problem is, whenever an extra GET variable is added to the end, it no longer recognises that the URL is a match.

so
http://www.example.com/dir/page.php?var=1
matches, but
http://www.example.com/dir/page.php?var=1&sid=1
doesn't.

does anyone know a way of changing the code so it checks against the URL and first GET variable, plus any number of random GET variables after it?

thanks!

StoutFiles

2:24 pm on Apr 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A better way to solve this problem would be to not use javascript at all. Call a function to check the value of var on the pages you need it checked.

Fotiman

2:28 pm on Apr 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This little bit of code allows you to access the query string parameters much like you would in PHP:

(function () {
var $_GET = (function () {
var a, i, o = (window.location.search.substring(1)).split('&');
for (i in o) {
a = o[i].split('=');
o[a[0]] = a[1];
}
return o;
})();
// Now the GET querystring values are accessible via $_GET['key']
// for example, given querystring ?name=Fotiman&var=1&foo=bar
// if ($_GET['var'] == 1) {
// dosomething
// }
if($_GET['var'] == 1) {
alert('success');
}
else {
alert('no luck');
}
})();

londrum

2:31 pm on Apr 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



fotiman looks good, will give that a go. thanks