jQuery TipsProgramming Tips

How To Get $_GET and $_POST Variables In Javascript

Retrieving the GET and POST superglobal headers in Javascript is actually very easy, although most of us are spoiled by the method used by PHP ($_GET and $_POST).

Although it takes a few more lines of code, you can mimic PHP.

How To Get $_GET in Javascript

var $_GET = {};

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
        return decodeURIComponent(s.split("+").join(" "));

    $_GET[decode(arguments[1])] = decode(arguments[2]);

You can now access the $_GET variables like so:


How To Get $_POST in Javascript

$_POST is handled a little differently (with the help of PHP). This cross-language fix allows you to store the PHP superglobal $_POST into a Javascript variable. You can also use this method for $_GET.

var $_POST = ;


Happy coding!

Join the discussion 10 Comments

Leave a Reply

Allen Gingrich

Author Allen Gingrich

Allen has worked on the web for over a dozen years. Like many young entrepreneurs, he began with a small workspace in his basement, where he eventually formed Ideas and Pixels. The rest, well, is history. Allen enjoys fine wines, weight training, and beautiful, semantic code.

More posts by Allen Gingrich

Join the discussion 10 Comments

Leave a Reply