Click to See Complete Forum and Search --> : collecting url variables


Count_Rugen
05-27-2003, 11:03 PM
i am developing a site using php.
when i pass variables in the url, as such;
testpage.php?variable=true
am i then able to retrieve this variable using javascript?

khalidali63
05-27-2003, 11:13 PM
yes

var temp= window.location.search;

at this point
temp = "variable=true"

Count_Rugen
05-28-2003, 12:39 AM
thanks for the reply
one more question then if you dont mind.
if i am passing the variables as such:

testpage.php?id=10&var=12

then using the command you gave, i create the variable

temp = '?id=10&var=12'

if i am interested in retrieving the value for the var variable, how can i get it from this string?

khalidali63
05-28-2003, 06:56 AM
you can use String.split() function it will separate the whole url into array,Suppose in the example you have above

url = "testpage.php?id=10&var=12 "

var srchStr = window.location.search

srchStr contains the following text at this point
"id=10&var=12"
you can further split this string into some data that makes more sense and easy to manipulate

var data srchStr.split("&");
now data is an array that has 2 elements
at index=0
data[0] = "id=10"
and at index =1
data[1] = "var=12"
it just is more string parsing at this level nothing much,hope this helps

Charles
05-28-2003, 07:09 AM
I generally use Regular Expressions to clean that up just a wee bit. (And I do mean "wee").

<script type="text/javascript">
<!--
function Query () {
if (self.location.search) {
this.query = self.location.search.replace(/^\?/, '');
var pairs = this.query.split('&');
for (i=0;i<pairs.length;i++) {if (/(.*)=(.*)/.test(pairs[i])) this[unescape(RegExp.$1)] = unescape(RegExp.$2)};
}
}

Query.prototype.toString = function () {return this.query}
// -->
</script>