使用JS检查查询字符串是否为空值

时间:2010-08-16 11:49:16

标签: javascript

是否可以使用javascript检查查询字符串是否为空值?你能告诉我一个如何做到这一点的例子吗?

与此代码段的IF子句类似:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["someParameter"]))
        {
            DataListWithLinksID.Attributes.Add("style", "display:none");
        }
    }

1 个答案:

答案 0 :(得分:1)

我用这个

<html>
<head>
<script>
function parseQString() {
  var qs = new Array();
  var loc = location.search;
  if (loc) {
    loc = loc.substring(1); // lose the leading ?
    var parms = loc.split('&');
    for (var i=0;i<parms.length;i++) {
      nameValue = parms[i].split('=');
      qs[nameValue[0]]=(nameValue.length == 2)? unescape(nameValue[1]):null; // use null or ""
    }
  }
  return qs;
}
function getQSParm(parm) {
  return (qs[parm])?qs[parm]:null
}
var qs = parseQString();
// try getQstringValue.html?amount=23&emptyparm=&parmwithoutequals
alert(getQSParm("amount"));
alert(getQSParm("emptyparm"));
alert(getQSParm("parmwithoutequals"));

// your example:

window.onload=function() {
  if (getQSParm("someParameter")) {
      document.getElementById('DataListWithLinksID').style.display='none':
    }
}

</script>
</head>
<body>
</body>
</html>