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.

4

3 回答 3

3

使用正则表达式匹配。

if(window.location.href.match(/\?user=[^&]/) {
    start();
} else {
    alert("No user.");
}
于 2013-11-14T20:16:15.893 回答
0
var user = (window.location.search.match(/user=(\w+)/) || [])[1];

if(user) {
    start();
} else {
    alert('No user.');
}
于 2013-11-14T20:27:52.457 回答
0

你可以只使用这个正则表达式:

/user=\w+(&|$)/.test(window.location.search)
于 2013-11-14T20:18:03.320 回答