Edward Tanguay Edward Tanguay - 1 year ago 105
Javascript Question

how to get GET and POST variables with JQuery?

How do I simply get

values with JQuery?

What I want to do is something like this:

$('#container-1 > ul').tabs().tabs('select', $_GET('selectedTabIndex'));

Answer Source

For GET parameters, you can grab them from document.location.search:

var $_GET = {};

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

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


For POST parameters, you can serialize the $_POST object in JSON format into a <script> tag:

<script type="text/javascript">
var $_POST = <?php echo json_encode($_POST); ?>;


While you're at it (doing things on server side), you might collect the GET parameters on PHP as well:

var $_GET = <?php echo json_encode($_GET); ?>;

Note: You'll need PHP version 5 or higher to use the built-in json_encode function.

Update: Here's a more generic implementation:

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {},
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
            = decodeURIComponent(tokens[2]);

    return params;

var $_GET = getQueryParams(document.location.search);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download