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:

alert($_GET['some_variable']);

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.

<script>
var $_POST = ;

document.write($_POST["test"]);
</script>
Happy coding!

About The Author: 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.

  • soiqualang_chentreu

    Thank you very much! It held me alot ^^

  • yann

    Thank you very much, works great

  • Zeh7o

    Thank you!

  • http://scraptings.blogspot.com Black Area

    This is working. Thank’s :)))

  • Danilo

    Thank you! the method for GET is working but the method for POST not… but held me alot!!!

  • vientohowai

    Although POST does but work, It is a good trick for GET!!
    By definition, I thiknk POST is no way to retrieve without server side coding as below discussion…
    http://stackoverflow.com/questions/10896854/simple-html-post-without-any-server-scripting-can-be-done-using-js

  • Daniel Laidler

    //EDIT (remember to remove spaces between

    var $_POST = ;

    document.write();

  • Muhammad Umar

    Hi, Thank zou for such an amaying solution. Its working fine.

  • Buy Contact Lenses

    I hope you will keep on submitting new articles or blog posts & thank you for sharing your great experience with us.

  • Acuvue Moist Contact Lens

    That is an extremely smart written article. I will be sure to bookmark it and return to learn extra of your useful information. Thank you for the post. I will certainly return.