2

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();

Iladarsda
  • 10,640
  • 39
  • 106
  • 170

5 Answers5

3

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'){
kamui
  • 3,339
  • 3
  • 26
  • 44
  • your example works great. How about passing `$.getUrlVar('name')` value into `OnLoad` function? – Iladarsda Nov 04 '11 at 11:22
  • if you add the line $(document).ready(function() { methodForDoingCoolStuffForFrank($.getUrlVar('name')); /* the method that would be called from onload */ }); Then it gets called when the page is ready and will be able to do something based on the name value. – kamui Nov 04 '11 at 14:57
2

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.

Connell
  • 13,925
  • 11
  • 59
  • 92
2

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";

Manse
  • 37,765
  • 10
  • 83
  • 108
  • Looks complicated. Does it return urlParams as an array? In my case there will be only one param. – Iladarsda Nov 04 '11 at 11:08
  • Its not complicated - its a split of the querystring - I have updated the answer to give you a usage example. – Manse Nov 04 '11 at 11:11
  • usage is simple. But the fetching method it self looks very complicated - not my pay grade! :) – Iladarsda Nov 04 '11 at 11:15
1

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>"; 
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

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')

Baz1nga
  • 15,485
  • 3
  • 35
  • 61