如何使用地址栏将值从javascript传递给php

I have .js and .php files and html pages. I am including js files in html files and php files in js files.

I want to pass 'cat' value from js file to php file using address bar when I go to this page;

/demo/convert.html?cat=volume

But I have no idea how to do this.

By the way, this is a blacberry project and I am not sure if I can use address bar to pass value. Any idea is welcome.

Test this sample code with an URL like : http://sputnick-area.net/test/index.php?foobar=works_as_a_charm

<?php

$var = $_GET['foobar'];

echo <<<EOF
<html>
<head>
<title></title>
</head>
<body>
demo of using PHP GET variable in Javascript :
<script type="text/javascript">
alert("$var");
</script>
</body>
</html>
EOF

?>

Edit :

if you'd like to handle GET variables from within JavaScript, consider the following HTML + JavaScript sample : http://sputnick-area.net/test/index.html?foobar=works_as_a_charm

<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var vars = [], hash;
var hashes = window.location.href.slice(
    window.location.href.indexOf('?') + 1
).split('&');

for(var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
}

alert(vars['foobar']);
</script>
</body>
</html>

Sure you can. When your JS function is called, you would have to do something like this:

function someFunction(someParameters) {
    //Do whatever you need to do
    window.location = "/demo/convert.html?variableName=" + variable;
}

This will cause a page reload with the new variable accessible through PHP in the $_GET array. For example:

<?php
$name = $_GET['variableName'];
if(length($name) < 3) {
    echo "That is a short name!";
}
?>

A page reload (used here), is necessary to send value to PHP as it is run server side. Your only other solution would be to use AJAX and load page content dynamically. This, however, would be the simplest solution.

EDIT:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

var urlvariable = getUrlVars()['variableName'];