0

I have a url which could be www.website.com/order/?pg3=2 or www.website.com/orderstatus/?pg2=

There could be several combinations and numbers at the end, all i want to really do is check is pg2 or pg3 are empty?

Im not sure how to actually access the post variables in the url, sometimes the url might not have the variables at all tho ?

$(document).ready(function () {

  if(pg2 != ''){
    //Does a click
    $("#add2").click();
  }
  if(p3 != ''){
    $("#add3").click();
  }

});

how do i check if the pg2 is empty in javascript on the page load?

Beginner
  • 28,539
  • 63
  • 155
  • 235
  • possible duplicate of [Get query string values in JavaScript](http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript) – Grant Thomas Mar 28 '12 at 10:55

2 Answers2

0

You can parse querystring with plain old js and then check if particular param populated The function below will give you a params array

  var queryString = window.location.href.replace(/^[^\?]+\??/,'');

var params = parseQuery( queryString );
function parseQuery ( query ) {
       var Params = new Object ();
       if ( ! query ) return Params; // return empty object
       var Pairs = query.split(/[;&]/);
       for ( var i = 0; i < Pairs.length; i++ ) {
          var KeyVal = Pairs[i].split('=');
          if ( ! KeyVal || KeyVal.length != 2 ) continue;
          var key = unescape( KeyVal[0] );
          var val = unescape( KeyVal[1] );
          val = val.replace(/\+/g, ' ');
          if ( ! Params[key] ) Params[key] = new Array ();
          Params[key].push( val );
       }
       return Params;
    }
Shaun Hare
  • 3,771
  • 2
  • 24
  • 36
0

Im not sure how to actually access the post variables in the url

First off, if they're in the URL, they're GET variables, not POST variables (which are in the body of a request sent to the server). Specifically, they're values on the query string.

You can access the query string via location.search. Unfortunately, it is as it appears in the address bar, rather than nicely parsed out for you, but there are plug-ins you can use that will handle parsing it out for you, like this one which would give you access to your pg2 variable like this:

var pg2 = $.url(location).param('pg2');

That pg2 variable will be undefined if there is no matching parameter on the query string.

That's just one example, there are several query string / URL parsing plug-ins available, or of course you can roll your own. Here's one I did a couple of years ago:

/**
 * Split up the given string (for instance, window.location.search) into component parts.
 *
 * @param   str         The string
 * @param   The component parts as keys on an object; if the query string has repeated entries,
 *          that key's value will be an array; a key with no value is present with the value
 *          `undefined`.
 */
$.splitQueryString = splitQueryString;
function splitQueryString(str) {
    var entries, parts, result, entryIndex, key, newVal, oldVal;

    // We return the result as an object
    result = {};

    // Skip a leading ? if any
    if (str.charAt(0) === '?') {
        str = str.substring(1);
    }

    // Strip anything after the hash symbol
    index = str.indexOf('#');
    if (index >= 0) {
        str = str.substring(0, index);
    }

    // decodeURIComponent won't do '+' => ' ', so do it
    str = str.replace(/\+/g, ' ');

    // Split into entries
    entries = str.split('&');
    for (index = 0; index < entries.length; ++index) {
        parts = entries[index].split('=');
        key = decodeURIComponent(parts[0]);
        newVal = parts[1];
        if (typeof newVal !== 'undefined') {
            newVal = decodeURIComponent(newVal);
        }
        if (key in result) {
            oldVal = result[key];
            if ($.isArray(oldVal)) {
                oldVal.push(newVal);
            }
            else {
                result[key] = [oldVal, newVal];
            }
        }
        else {
            result[key] = newVal;
        }
    }

    // Done
    return result;
}

Usage:

var pg2 = $.splitQueryString(location.search).pg2;
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875