1

This question (or similar) seems to get asked often, but I've tried many ways to check if the value returned from a function I have, is null or not; to no avail.

My function, get's URL parameters by name:

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

However, obviously a parameter may not be there, so I need to do a check on the value returned.

Doing console.log( getURLParameter('client') ); returns null...but doing null checks does not work.

I have tried the following:

if ( getURLParameter("client") !== null ) {

    alert("It's there matey!");

}

if ( getURLParameter("client") != null ) {

    alert("It's there matey!");

}

if ( ! getURLParameter("client") ) {

    alert("It's there matey!");

}

None of these seems to work for me.

Is there anywhere I am going wrong? I can do this either in vanilla JS, or using jQuery library.

Ian
  • 50,146
  • 13
  • 101
  • 111
Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140
  • Trust me, there's a better function to get a URL parameter (I know this function is from http://stackoverflow.com/questions/831030/how-to-get-get-request-parameters-in-javascript ). You shouldn't need to run a regex every time you call the function. – Ian Jun 28 '13 at 16:36
  • 3
    All that was required to figure this out was straight-forward debugging. Break the statement up in to its individual parts and walk through the code with the debugger built into your browser. Doing so will be *dramatically* faster than typing in a question on SO. – T.J. Crowder Jun 28 '13 at 16:38
  • I would suggest looking at http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values but not necessarily liking the accepted answer. (I prefer the second highest voted question) – Ian Jun 28 '13 at 16:45

3 Answers3

5

The problem is decodeURI, which is returning the string "null" when you pass null into it. The solution is to do the null check before calling decodeURI. You can find this by just breaking the function up into its parts:

function getURLParameter(name) {
    var rex = RegExp(name + '=' + '(.+?)(&|$)');
    var result = rex.exec(location.search);
    var rv = decodeURI(
      (result||[,null])[1]
    );
    return rv;
}

...and walking through it in the debugger built into your browser.

Or for those who prefer console.log-style debugging:

function getURLParameter(name) {
    var rex = RegExp(name + '=' + '(.+?)(&|$)');
    var result = rex.exec(location.search);
    console.log("typeof result = " + typeof result);
    console.log("result = " + result);
    var rv = decodeURI(
      (result||[,null])[1]
    );
    console.log("typeof rv = " + typeof rv);
    return rv;
}

...which for getURLParameter("foo") shows us:

typeof result = object
result = null
typeof rv = string
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

That's because the value your function is returning is a string ('null') and therefore it fails with your tests.

var result = getURLParameter("client");
console.log('Value: ' + result);                // 'null'
console.log('Data type: ' + typeof(result));    // string
console.log(result!==null);                     // true
console.log(result!=null);                      // true
console.log(!result);                           // false

It returns a string because you are passing the null value as parameter to the decodeURI function, which converts to the string 'null'.

So, I edited your function getURLParameter to check if the value of the param is null before calling decodeURI. See below:

function getURLParameter(name) {
    var param = RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || null;
    return param===null ? null : decodeURI(param[1]);
}

Now, let's run the tests again:

var result = getURLParameter("client");
console.log('Value: ' + result);                // null
console.log('Data type: ' + typeof(result));    // object
console.log(result!==null);                     // false
console.log(result!=null);                      // false
console.log(!result);                           // true
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
1

try

if ( getURLParameter("client") != 'null' ) {

    alert("It's there matey!");

}
Chaos
  • 374
  • 2
  • 11
  • 2
    And what if the value **actually is** "null"? – Ian Jun 28 '13 at 16:38
  • isn't the `!=` operator casting the expressions? so "null" and 'null' is no difference. – Chaos Jun 28 '13 at 16:40
  • I don't understand your point. I'm saying what if the querystring is this: `?p1=null`? If you used `getURLParameter("p1")`, the result would be `"null"`, and your solution would fail to see it – Ian Jun 28 '13 at 16:41
  • 2
    ...and even if it weren't for @Ian's very good point, that test doesn't work anyway: `null != 'null'` returns `true`. – T.J. Crowder Jun 28 '13 at 16:42