0

I have this piece of code, which checks the address bar for the string ?user=:

if(window.location.href.indexOf("?user=") > -1) {
    start();
} else {
    alert("No user.");
}

But actually I would like to check, if the ?user= has something after it, for example a name. I would be glad if you could help me out.

danrodi
  • 296
  • 1
  • 4
  • 24
  • 2
    You could do that (see [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript)). However, that smells like you're implementing a very bad security system... – bfavaretto Nov 14 '13 at 20:14
  • In this case security doesn't matter, however thanks for the tip. – danrodi Nov 14 '13 at 20:16
  • `/\?user\=./.test(window.location.search)` checks if any character comes after the equal sign ? – adeneo Nov 14 '13 at 20:18

3 Answers3

3

Use a regular expression match.

if(window.location.href.match(/\?user=[^&]/) {
    start();
} else {
    alert("No user.");
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You could just use this regular expression:

/user=\w+(&|$)/.test(window.location.search)
m1.
  • 1,265
  • 9
  • 9
0
var user = (window.location.search.match(/user=(\w+)/) || [])[1];

if(user) {
    start();
} else {
    alert('No user.');
}
ArrayKnight
  • 6,956
  • 4
  • 17
  • 20