jQuery - check for POST in the URL

1.9k Views Asked by At

Is there any way to check with jQuery for POST within the URL similar to the PHP way?

PHP version:

if(!empty($_GET)) { // do stuff }

Sorry. I mean GET not POST.

I'm looking to fetch the URL on the page load $(document).ready( function() { // here });

Example URL: http://www.domain.com?a=1

And than past value of the var a into the function OnLoad();

5

There are 5 best solutions below

2
On BEST ANSWER

extend jquery to get the url vars

$.extend({
  getUrlVars: function(){
    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];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

then you can call

if($.getUrlVars().length > 0){ // do something }

you can also query a specific value by

if($.getUrlVar('name') == 'frank'){
0
On

No. POST data is not accessible to JavaScript.

You would have to do a check in PHP, and output some JavaScript setting a variable to let it know about the result.

e.g.

if(!empty($_POST))
  echo "<script type='text/javascript'> post_not_empty = true; </script>"; 
0
On

New Answer

Just use JavaScript to check if a query string there or not.

if(location.search.length > 0)
    alert('Horay horah!');

If you need to get the actual data within the query string you could use the Query String Object jQuery plugin, or you go about parsing the location.search text.

3
On

If you want to get the QueryString parameters using JQuery I suggest you use the following code - it stores the parameters in a global variable (cue the complaints !) and then parses the querystring into that variable:

var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();

Example of usage :

URL = http://www.mywebsite.com/open?abc=123&def=234

alert(urlParams["abc"]);

will display an alert with the value "123";

0
On

A jquery plugin to get this done:

/*
jQuery Url Plugin
* Version 1.0
* 2009-03-22 19:30:05
* URL: http://ajaxcssblog.com/jquery/url-read-get-variables/
* Description: jQuery Url Plugin gives the ability to read GET parameters from the actual URL
* Author: Matthias Jäggli
* Copyright: Copyright (c) 2009 Matthias Jäggli 
* Licence: dual, MIT/GPLv2
*/
(function ($) {
    $.url = {};
    $.extend($.url, {
        _params: {},
        init: function () {
            var paramsRaw = "";
            try {
                paramsRaw =
                    (document.location.href.split("?", 2)[1] || "").split("#")[0].split("&") || [];
                for (var i = 0; i < paramsRaw.length; i++) {
                    var single = paramsRaw[i].split("=");
                    if (single[0])
                        this._params[single[0]] = unescape(single[1]);
                }
            }
            catch (e) {
                alert(e);
            }
        },
        param: function (name) {
            return this._params[name] || "";
        },
        paramAll: function () {
            return this._params;
        }
    });
    $.url.init();
})(jQuery);

usage: $.param("key") in your example $.param('a')