1

First off, I do not want what is in the URL query. I want what PHP see's in the$_GET array.

This is because the URL query will not show all the params if mod_rewrite has been used to make pretty URLs

So is there a way to get the query string that would match exactly what is in the php $_GET array?

--

I came up with a way myself using PHP and JavaScript like so:

function query_string()
{
    <?php
        function assoc_array_to_string ($arr)
        {
            $a = array();
            foreach($arr as $key => $value)
            {
                $str = $key.'='.$value;
                $a[] = $str;
            }
            return implode("&",$a);
        }
    ?>
    return '<?=urlencode(assoc_array_to_string($_GET))?>';
}

...but I need to do this with just javascript if possible because I can't put PHP code in a .js file.

JD Isaacks
  • 51,154
  • 89
  • 267
  • 413
  • possible duplicate of [Parse query string in JavaScript](http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript) – kennytm Jul 20 '10 at 17:42
  • (Javascript can't reach the server. It won't know what `mod_rewrite` is doing. The above is the best solution you can do without server interaction.) – kennytm Jul 20 '10 at 17:43
  • @KennyTM This isn't a duplicate because the above question is asking how to get what is in the url query string, I am not. I didn't know if they were stored anywhere retrievable from JS/DOM. I guess there is not, but a solution like Jhongs is better for my question then the link above. – JD Isaacks Jul 20 '10 at 17:49

3 Answers3

3

Won't JavaScript "only see" the query string? How would client-side script know about any rewrite rules?

The only way I can think of is to use PHP -- echo it into a variable in an inline script in your main page rather than the JS file.

Jhong
  • 2,584
  • 18
  • 19
3

In your page <head>:

<script type="text/javascript">
var phpQueryParams = <?php print json_encode($_GET); ?>
</script>

Assuming at least PHP 5.2, otherwise use an external package

Nick
  • 11,233
  • 1
  • 34
  • 46
  • Although the returned JSON should be safe to use in terms of possible malicious strings, you should perhaps wrap the script content in , combined with a str_replace to turn ]]> into ]"+"]> so that a string containing ]]> doesn't end the CDATA block. If you decide not to use a JSON encoder you should be even more alert to the ways that a malicious query string could inject script into your page. – Nick Jul 21 '10 at 12:04
1

The query string is found in window.location.search, but that's the raw query string. So if you run something like this:

(function () {
    QueryStr = {}
    QueryStr.raw = window.location.search.substr(1);
    var pairStrs = QueryStr.raw.split('&');
    QueryStr.val = {}
    for(var i=0,z=pairStrs.length; i < z; i++) {
        var pair = pairStrs[i].split('=');
        QueryStr.val[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
    }
})();

You'd have something very much like $_GET in QueryStr.val.

Of course, you mention that you've mixed things up a bit using mod_rewrite, which is cool, but since we don't know your rewrite scheme, we can't help specifically with that.

However... you know your rewrite scheme, and you could probably modify the code I gave above to operate on some other part of window.location. My bet is that you'd want to split window.location.pathname on the / character instead of &.

Weston C
  • 3,480
  • 2
  • 21
  • 30
  • Thanks, I could possibly (since I know the mod_write scheme) extract it from the URL using regex or something. However, I wanted a less ad-hoc way since I am panning on reusing this on multiple pages with different schemes. I think I may do something like Jhong suggests and just require a parameter in a function that can be passed in from outside the .js file with php. But thanks for the suggestion it is an idea I hadn't even thought of! – JD Isaacks Jul 20 '10 at 17:56