0

How can I make a javascript if function where if the query string does show as something other than undefined. Here is the code:

    var getQueryString = window.location.href.split("?")[1];
alert(getQueryString);  

Basically I wanted to do an if function in javascript. Here is the code:

    if (getQueryString = 1){
          //add code
    }

How can I set my javascript function?

  • Im a bit lost, can you elaborate on what you're trying to accomplish – tymeJV Apr 08 '14 at 19:46
  • basically if I there is no query string on the url and the alert comes up as undefined I wanted to do an if undefined. – user3499262 Apr 08 '14 at 19:48
  • Err, undefined is falsy, no need to check for 1 (or set to 1) ? – adeneo Apr 08 '14 at 19:50
  • See [link](http://stackoverflow.com/questions/519145/how-can-i-check-whether-a-variable-is-defined-in-javascript) – Always Learning Apr 08 '14 at 19:52
  • 1. You should not name variables as if they were methods. Verbs should be used for method names only. 2. You are assigning `1` here; for comparison use `==` or `===`. 3. You need to compare against `undefined`; for compatibility and safety (the `undefined` property has not always been read-only), use the `typeof` operator instead. – PointedEars Apr 08 '14 at 19:52

2 Answers2

1

I would use location.search and check it's length instead of your approach.

if (window.location.search.slice(1).length > 0) {
  doSomething();
}

Currently, your code will always execute because you're setting getQueryString to 1 instead of comparing it. Even if you were comparing it, it would still be false since getQueryString is a string and not a number.

Bill Criswell
  • 32,161
  • 7
  • 75
  • 66
  • `location.search` may be `"?"` if there is no query part. Also, it should be `window.location.search`. – PointedEars Apr 08 '14 at 19:54
  • "May be" does not mean "must be", so in theory you need to trim the leading `?` and then compare the length. Everything is easier with regular expression matching, though. – PointedEars Apr 08 '14 at 19:58
  • `location.search` returns an empty string even if there's just a `?` at the end of the url. I changed it back to 0. – Bill Criswell Apr 08 '14 at 20:01
  • 1
    You are jumping to conclusions. This feature originates from DOM Level 0. There is no specification (that I am aware of), and implementations have been varying over the years. – PointedEars Apr 08 '14 at 20:04
  • You're totally right. I just tried it in IE9 and it returned just the `?`. I adjusted my code. – Bill Criswell Apr 08 '14 at 20:11
  • Adjusted. As far as I know it's just a matter of taste, though. – Bill Criswell Apr 08 '14 at 20:18
  • It is a capital mistake to assume that the ECMAScript Global Object would in general be the same object as the `window` object. And consider local variables. – PointedEars Apr 08 '14 at 23:02
0

The bellow code should work for you

var getQueryString = window.location.href.split("?")[1];

if (typeof getQueryString !== 'undefined')
{
    //Your code goes here
} else [
  alert("Query String not set");
}
CGeorges
  • 488
  • 4
  • 16